I’m trying to retrieve data from Firebase. Here’s my code snippet
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
// DatabaseReference ref = FirebaseDatabase.instance.ref();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
final docRef = FirebaseFirestore.instance.collection('data').doc("rules");
docRef.get().then(
(DocumentSnapshot doc) {
final data = doc.data() as Map<String, dynamic>;
return MyHomePage(title: 'ARNET Helper',
rules: data['ruleslist']);
},
onError: (e) => print("Error getting document: $e"),
);
return Spinner(text: "Unable to retrieve data");
}
}
Here’s the corresponding Firebase database screenshot
I do have the google-services.json added to the android/app folder. But with the above snippet, lines from the "then" block don’t seem to get hit and the spinner (i.e return Spinner(text: "Unable to retrieve data");) is always returned.
I do have these lines added to AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
I tried to debug it. But the breakpoint at line 46 is never hit. Instead the execution goes straight to line 52. What am I missing?
I’ve even tried using FutureBuilder as suggested by one of the commenters. I always get "Something went wrong.." spinner
@override
Widget build(BuildContext context) {
CollectionReference data = FirebaseFirestore.instance.collection('data');
return FutureBuilder<DocumentSnapshot>(
future: data.doc('rules').get(),
builder:
(BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.hasError) {
return Spinner(text: 'Something went wrong...');
}
if (snapshot.hasData && !snapshot.data!.exists) {
return Spinner(text: 'Document does not exist...');
}
if (snapshot.connectionState == ConnectionState.done) {
Map<String, dynamic> data = snapshot.data! as Map<String, dynamic>;
return MyHomePage(title: 'ARNET Helper', rules: data['ruleslist'].entries.toList() as List<dynamic>);
}
return Spinner(text: 'Loading...');
},
);
}
}
3
Answers
Not sure what the issue was but the config below worked.
pubspec.yaml
FutureBuilder query
Did you initialize the Firebase Instance?
The problem is that
get()
is an asynchronous operation, and you can’t return widgets asynchronously inbuild
.The simplest way to fix this is to use a
FutureBuilder
: