When I try to use futurebuilder for the whole scaffold I get the error message "error: The body might complete normally, causing 'null' to be returned, but the return type, 'Widget', is a potentially non-nullable type."
(I tried hardcoding the scaffold to try to avoid it being perceived as null, but that didn't work)
@override
Widget build(BuildContext context) {
routeData = ModalRoute.of(context)?.settings.arguments as Map;
print(routeData);
final DocumentReference docRef = FirebaseFirestore.instance.collection("jet").doc(routeData['id']);
FutureBuilder<DocumentSnapshot>
(
future: docRef.get(),
builder: (context, snapshot)
{
return Scaffold
(
appBar: AppBar
(
title: Text("Waiting..."),
),
body: Center(child: CircularProgressIndicator()),
);
The error is due to the build method as it must return the non nullable widget and if you are using future builder so there will be chances that future might not have completed yet.
you have to ensure that FutureBuilder always returns a valid Widget even if the future hasn't completed yet.
You need to modify your code like this:
@override
Widget build(BuildContext context) {
routeData = ModalRoute.of(context)?.settings.arguments as Map;
print(routeData);
final DocumentReference docRef =
FirebaseFirestore.instance.collection("jet").doc(routeData['id']);
return FutureBuilder<DocumentSnapshot>(
future: docRef.get(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Scaffold(
appBar: AppBar(
title: Text("Waiting for future..."),
),
body: Center(child: CircularProgressIndicator()),
);
} else if (snapshot.hasError) {
return Scaffold(
appBar: AppBar(
title: Text("Error"),
),
body: Center(child: Text("Error: ${snapshot.error}")),
);
} else {
return Scaffold(
appBar: AppBar(
title: Text("Data Loaded"),
),
body: Center(child: Text("Data has been loaded")),
);
}
},
);
}