Home > other >  QuerySnapshot<Object?>?' is nullable
QuerySnapshot<Object?>?' is nullable

Time:12-15

I have a problem with Firebase in Flutter

This is the code:

StreamBuilder<QuerySnapshot>(
              stream: FirebaseFirestore.instance.collection("tasks").snapshots(),
              builder:(context, AsyncSnapshot<QuerySnapshot>snapshot){
                if(!snapshot.hasData) return LinearProgressIndicator();
                return Expanded(
                  child: _buildList(snapshot.data)
                );
              }
            )

And the console tell me:

Error: The argument type 'QuerySnapshot<Object?>?' can't be assigned to the parameter type 'QuerySnapshot<Object?>' because 'QuerySnapshot<Object?>?' is nullable and 'QuerySnapshot<Object?>' isn't. lib/main.dart:82

  • 'QuerySnapshot' is from 'package:cloud_firestore/cloud_firestore.dart' ('/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-3.1.4/lib/cloud_firestore.dart'). package:cloud_firestore/cloud_firestore.dart:1
  • 'Object' is from 'dart:core'. child: _buildList(snapshot.data)

I can't understand the problem. Thanks

CodePudding user response:

snapshot.data could be null, that means it is nullable. You already made sure it won't be null by adding an if (snapshot.hasData), but dart doesn't know that. So in order to let dart know that you've made sure a nullable value is not null, you should add a null check operator (!):

_buildList(snapshot.data!);

This means you will get an error if snapshot.data is null, but in this case it won't be because you already checked before.

  • Related