简体   繁体   中英

Null check operator used on a null value Flutter

I know this problem is related with dart null safety but i am unable to fix this issue as i am getting this 'Null check operator used on a null value Flutter' error.Please help me to fix this

my code is:

class _ListStudentPageState extends State<ListStudentPage> {
  final Stream<QuerySnapshot> studentStream =
  FirebaseFirestore.instance.collection('students').snapshots();
  @override
  Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      stream: studentStream,
      builder:(BuildContext context , AsyncSnapshot<QuerySnapshot> snapshot){
      if(snapshot.hasError)
        {
          print('something went wrong');
        }
      if(snapshot.connectionState == ConnectionState.waiting)
        {
          return const Center(
            child: CircularProgressIndicator(),
          );
        }
       final List storeDocs =[];
      snapshot.data!.docs.map((DocumentSnapshot document){
        Map a = document.data() as Map<String , dynamic>;
        storeDocs.add((a));
        a['id'] = document.id;
      }).toList();

I would try this:

      final List storeDocs =[];
      if(snapshot.data=null){
      snapshot.data!.docs.map((DocumentSnapshot document){
        Map a = document.data() as Map<String , dynamic>;
        storeDocs.add((a));
        a['id'] = document.id;
      }).toList();
}

or in prettier way

      final List storeDocs =[];
      snapshot.data?.docs??[].map((DocumentSnapshot document){
        Map a = document.data() as Map<String , dynamic>;
        storeDocs.add((a));
        a['id'] = document.id;
      }).toList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM