I am able to do a get query into firebase and use the print function to confirm it is working. However I don’t understand how to access the retrieved variable elsewhere in the code. I am getting an error "Error: The getter ‘data’ isn’t defined for the class ‘ItemDetails’" for the attempted text field below. Please could someone point me in the right direction? Thanks
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
class ItemDetails extends StatelessWidget {
ItemDetails(this.itemId, {Key? key}) : super(key: key) {
var db = FirebaseFirestore.instance;
final docRef = db.collection("collection_name").doc("frngqy6aaxb88mw2n09p6e9qhkrkhhmmywrahm8f79e4myuwku6");
docRef.get().then(
(DocumentSnapshot doc) {
final data = doc.data() as Map<String, dynamic>;
print(data["name"]); // !!! THIS WORKS !!!
return data;
},
onError: (e) => print("Error getting document: $e"),
);
}
String itemId;
late DocumentReference _reference;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Item detail"),
),
body: Center(
child: Text(data["name"]) // !!! THIS LINE GIVES THE ERROR !!!
)
);
}
}
I tried adding the "return data;" line but it didn’t help
2
Answers
In your current code, you are trying to access the data variable outside the then callback, which leads to the error you mentioned. The then callback is asynchronous, and its execution may not have been completed by the time the build method is called.
To handle asynchronous operations and update the UI with the retrieved data, you should use the FutureBuilder widget
If the operations you’re going to perform are asynchronous, you can convert them into a Future to manage them more easily. After converting them into Future, you can prepare the UI part where you will display the data by calling it in the desired page as FutureBuilder. I’ve implemented it for you in the example below. I hope it helps you 🙂