Reputation: 221
I have the following problem. Whenever the futurebuilder is re-executed, the following error appears in the terminal
NoSuchMethodError: The method '[]' was called on null
Nevertheless, the app works because an exception is always made.
Unfortunately, I have not found a suitable answer yet.
void initState(){
_getdata = getdata();
super.initState();
}
Future getdata() async {
DocumentReference qn = Firestore.instance.collection("data").document("datatxt");
return qn.get();
}
...
child: FutureBuilder(
future: _getdata,
builder: (BuildContext context, snap){
return Container(
width: MediaQuery.of(context).size.width,
color: Colors.green,
child: Text(snap.data['Text1']),
);
},
),
Another exception was thrown: NoSuchMethodError: The method '[]' was called on null
Upvotes: 0
Views: 1717
Reputation: 96
When you work with dart Futures (async/await) on Flutter you should prepare your widgets for 3 possible states.
I suggest you to work with FutureBuilder, StreamBuilder works the same way too, by checking the ConnectionState of the snapshot inside de builder function.
I use this approach:
FutureBuilder(
future: _getdata,
builder: (BuildContext context, AsyncSnapshot snap){
switch (snapshot.connectionState) {
// Uncompleted State
case ConnectionState.none:
case ConnectionState.waiting:
return Center(child: CircularProgressIndicator());
break;
default:
// Completed with error
if (snapshot.hasError)
return Container(Text(snapshot.error.toString()));
// Completed with data
return Container(
width: MediaQuery.of(context).size.width,
color: Colors.green,
child: Text(snap.data['Text1']),
);
)
References Youtube
Upvotes: 1