[英]How to retreive data from firestore flutter
I'm new into flutter and firebase integrations and I'm having some troubles to retreive all the data from the firebase collection.我是 flutter 和 firebase 集成的新手,我在从 firebase 集合中检索所有数据时遇到了一些麻烦。
I have tried this method:我试过这种方法:
getCollection() {
CollectionReference coleccion =
FirebaseFirestore.instance.collection('materias');
return Container(
child: StreamBuilder(
stream: coleccion.doc('aprobadas').snapshots(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.connectionState == ConnectionState.active) {
return Text(snapshot.data.data()['codigo'],
style: TextStyle(fontSize: 50, color: Colors.white));
} else {
return CircularProgressIndicator();
}
},
),
);
}
Now I'm a little bit frustrated because I have tried a differents methods and doesn't work.现在我有点沮丧,因为我尝试了不同的方法并且不起作用。
I really appreciate all the help.我真的很感谢所有的帮助。
Best regards最好的祝福
Data can be retrieved using the below code from firestore to flutter.可以使用以下代码从 firestore 到 flutter 检索数据。
One-time Read一次性阅读
call the Query.get
or DocumentReference.get
methods调用Query.get
或DocumentReference.get
方法
class GetUserName extends StatelessWidget {
final String documentId;
GetUserName(this.documentId);
@override
Widget build(BuildContext context) {
CollectionReference users = FirebaseFirestore.instance.collection('users');
return FutureBuilder<DocumentSnapshot>(
future: users.doc(documentId).get(),
builder:
(BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.hasError) {
return Text("Something went wrong");
}
if (snapshot.hasData && !snapshot.data!.exists) {
return Text("Document does not exist");
}
if (snapshot.connectionState == ConnectionState.done) {
Map<String, dynamic> data = snapshot.data!.data() as Map<String, dynamic>;
return Text("Full Name: ${data['full_name']} ${data['last_name']}");
}
return Text("loading");
},
);
}
}
Realtime changes实时变化
FlutterFire provides support for dealing with real-time changes to collections and documents. FlutterFire 支持处理集合和文档的实时更改。 A new event is provided on the initial request, and any subsequent changes to collection/document whenever a change occurs (modification, deleted or added).初始请求时提供新事件,以及在发生更改(修改、删除或添加)时对集合/文档的任何后续更改。
Both the CollectionReference
& DocumentReference
provide a snapshots() method which returns a Stream: CollectionReference
和DocumentReference
提供了一个返回 Stream 的 snapshots() 方法:
Stream collectionStream = FirebaseFirestore.instance.collection('users').snapshots();
Stream documentStream = FirebaseFirestore.instance.collection('users').doc('ABC123').snapshots();
You can use a StreamBuilder
.您可以使用StreamBuilder
。 That will be easy to understand.这将很容易理解。
StreamBuilder(
stream: FirebaseFirestore.instance.collection("collection").snapshot,
builder: (BuildContext context,snapshot) {
if(snapshot.hasdata!=true) {
return CircularProgressIndicator();
} else {
return ListView.builder(
itemcount:snapshot.data.docs.length,
builder(context,index) {
return Text(snapshot.data.docs[index].data()["filedname"]);
}
}
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.