0

Here is my code that produces a Null check operator used on a null value - error for this line: snapshot.data!.docs.map((DocumentSnapshot document) {

return ListView(
  children:
  snapshot.data!.docs.map((DocumentSnapshot document) {
    Map<String, dynamic> data =
    document.data()! as Map<String, dynamic>;


 

My full StreamBuilder code in body :

body: StreamBuilder<QuerySnapshot?>(
  stream: _usersStream,
  builder: (BuildContext context, AsyncSnapshot<QuerySnapshot?> snapshot) {

    if(snapshot.data !=null) {

      snapshot.data!.docs.map((DocumentSnapshot document) async {

        if (snapshot.hasError) {
          return Text('Something went wrong');
        }

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

      });
    }

    return ListView(
      children:
      snapshot.data!.docs.map((DocumentSnapshot document) {
        Map<String, dynamic> data =
        document.data()! as Map<String, dynamic>;
        return InkWell(
          child: Card(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10.0)),
            // elevation: 0.0,
            child: Container(
              child: Column(
                children: [
                  Container(
                    // margin: EdgeInsets.all(20.0),
                      width: double.infinity,
                      height: 260.0,
                      decoration: BoxDecoration(
                        color: Colors.blue,
                        image: DecorationImage(
                          image: NetworkImage(data['images'][0]),
                          fit: BoxFit.cover,
                        ),
                        borderRadius: BorderRadius.only(
                          topLeft: Radius.circular(15),
                          topRight: Radius.circular(15),),
                      )
                  ),
                  ListTile(
                    title: Text(data['catName']),
                    // subtitle: Text(data['images']),
                  ),
                ],
              ),
            ),
          ),
          onTap: () {
            //Go to the next screen with Navigator.push
            print(data['id']);

  

            Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => PostBook(id: data['id'])));

          },
        );
      }
      ).toList(),
    );
  },
),
Jahn E.
  • 1,228
  • 3
  • 6
  • 21
Muhammad Arslan
  • 939
  • 8
  • 13
  • 1
    About the 1st line after `builder`, `if(snapshot.data !=null) ` can be `if(snapshot.data ==null) `. also i would say 1st check connectionActivity, then error then data. – Md. Yeasin Sheikh Oct 14 '21 at 17:11
  • I'm closing this question as I think it's a duplicate of the linked post. If you disagree, please feel free to explain. – CopsOnRoad Oct 14 '21 at 17:22
  • 1
    This link will help you figuring it out on your own with multiple examples: https://firebase.flutter.dev/docs/auth/usage – Jahn E. Oct 15 '21 at 08:48
  • @YeasinSheikh thanks this helped me fix the problem. you should post this in the answer I will approve that. – Muhammad Arslan Oct 15 '21 at 10:41

0 Answers0