'How to implement null-safety in simple flutter-dart code with object snapshot.data?
I am new to flutter/dart coding, please help me solve the following:
Here is my code trying to fetch data from the FireStore collection "DinnerNames" but I get an analysis error on the snapshot.data object at the line:
itemCount: snapshot.data.documents.length
:
The problem:
An expression whose value can be 'null' must be null-checked before it can be dereferenced. Try checking that the value isn't 'null' before dereferencing it.
Here is the code sample generates the error:
CollectionReference dinners =
FirebaseFirestore.instance.collection('DinnerNames');
return Scaffold(
appBar: AppBar(
title: Text('My Dinner Voting App'),
),
body: StreamBuilder(
stream: dinners.snapshots(),
builder: (context, snapshot){
if (!snapshot.hasData) return const Text('Firestore snapshot is loading..');
if (!snapshot.hasError)
return const Text('Firestore snapshot has error..');
if (snapshot.data == null){
return const Text("Snapshot.data is null..");
}else{
return ListView.builder(
itemExtent: 80.0,
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) =>
_buildListItem(context, snapshot.data.documents[index]),
);
}
}
),
);
here is the flutter version:
dave@DaveMacBook-Pro firebasetest % flutter --version
Flutter 1.25.0-8.2.pre • channel beta • https://github.com/flutter/flutter.git
Framework • revision b0a2299859 (2 weeks ago) • 2021-01-05 12:34:13 -0800
Engine • revision 92ae191c17
Tools • Dart 2.12.0 (build 2.12.0-133.2.beta)
Solution 1:[1]
If you're sure that the object is not null
by the time you're using it, just insert a Bang !
operator to cast away nullability, something like this:
ListView.builder(
itemCount: snapshot.data!.docs.length, // <-- Notice '!'
)
Solution 2:[2]
you're already checking:
snapshot.data == null
but it seems the analysis is indicating that documents can also be null, so try to include also:
if (snapshot.data.documents != null) { ... do what you need }
or try to change this code:
if (snapshot.data == null){
return const Text("Snapshot.data is null..");
}
to this:
if (snapshot.data == null || snapshot.data.documents == null){
return const Text("Snapshot.data is null..");
}
Solution 3:[3]
If you explicitly tell dart/flutter that the type of the snapshot argument in the builder function will be 'AsyncSnapshot<QuerySnapshot>' you can let Flutter know that it will have certain attributes, like below. You may still have to use the '?.' operator and 'documents' has now been changed to 'docs':
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot){ // <-- here
if (!snapshot.hasData) return const Text('Firestore snapshot is
loading..');
if (!snapshot.hasError)
return const Text('Firestore snapshot has error..');
if (snapshot.data == null){
return const Text("Snapshot.data is null..");
}else{
return ListView.builder(
itemExtent: 80.0,
itemCount: snapshot.data?.docs.length,
itemBuilder: (context, index) =>
_buildListItem(context, snapshot.data?.docs[index]),
);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | CopsOnRoad |
Solution 2 | Gui Silva |
Solution 3 |