Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter – A non-null value must be returned since the return type 'Widget' doesn't allow null

I tried to run this code but it gives me the error provided in the title, also it says (The body might complete normally, causing ‘null’ to be returned, but the return type, ‘Widget’, is a potentially non-nullable type.), any help to solve this issue would be appreciated!

    Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      stream: FirebaseFirestore.instance.collection("chats").snapshots(),
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
        if(snapshot.hasError) {
          return Center (
            child: Text("There is a problem"),
          );
        }

        if(snapshot.connectionState == ConnectionState.waiting) {
          return Center (
            child: Text("Loading"),
          );
        }

        if(snapshot.hasData) {
          return CustomScrollView(
            slivers: [
              CupertinoSliverNavigationBar(
                largeTitle: Text("Chats"),
              ),

              SliverList(
                delegate: (
                    SliverChildListDelegate(
                      snapshot.data!.docs.map((DocumentSnapshot document) {
                        Map<String, dynamic> data = document.data()!as Map<String, dynamic>;
                        return CupertinoListTile(title: Text(data['title']),
                        );
                      }).toList())))
            ],
          );
      }
  });
  }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You need to return a widget after if(snapshot.hasData) .I guess you can simply remove if(snapshot.hasData) and your code would be fine. But for assurance, you can return an error widget after that .

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading