My futurebuilder gives error for few seconds on the screen and then show's the result here is the error logs:
The following NoSuchMethodError was thrown building FutureBuilder<DocumentSnapshot>(dirty, state: _FutureBuilderState<DocumentSnapshot>#fd0c5):
The method '[]' was called on null.
Receiver: null
Tried calling: []("title")
The relevant error-causing widget was:
FutureBuilder<DocumentSnapshot> file:///C:/Users/Admin/Desktop/Android/doorstep/lib/domain/repository/booking-repository.dart:7:12
When the exception was thrown, this was the stack:
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
#1 BookingRepository.getTitle.<anonymous closure> (package:doorstep/domain/repository/booking-repository.dart:11:39)
#2 _FutureBuilderState.build (package:flutter/src/widgets/async.dart)
#3 StatefulElement.build (package:flutter/src/widgets/framework.dart:4334:27)
#4 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4223:15)
Code
Widget getTitle(String id) {
return FutureBuilder(
future: Firestore.instance.collection('products').document(id).get(),
builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot){
return ListTile(
title: Text("${snapshot.data['title'].toString()}"),
);
}
);
}
You should check if the snapshot already has data before trying to display it, as per the documentation:
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
// return something
} else if (snapshot.hasError) {
// Manage error
} else {
// return something for the user to wait
}
}
Try
FutureBuilder<AsyncSnapshot<DocumentSnapshot>>(
builder: (context, snap) {
if (snap.connectionState != ConnectionState.done) {
//print('project snapshot data is: ${snap.data}');
return Text("loading");
} else {
if (snap.hasError) {
return Text([snap.error.toString()]);
}
else {
if (snap.hasData) {
return Text("${snap.data['title'].toString()}");
} else {
return Text("No DAta");
}
}
}
},
future:
Firestore.instance.collection('products').document(id).get(),
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With