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

Advertisements

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 :

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 .

Leave a ReplyCancel reply