[英]Get a list with all documents from a Firestore collection
I have this simple to query data from Firestore:我有这个简单的从 Firestore 查询数据:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
class TodoPage extends StatefulWidget {
const TodoPage({Key? key}) : super(key: key);
@override
State<TodoPage> createState() => _TodoPageState();
}
class _TodoPageState extends State<TodoPage> {
User? user = FirebaseAuth.instance.currentUser;
late final Stream<QuerySnapshot> _mainStream = FirebaseFirestore.instance
.collection('users')
.doc(user!.uid)
.collection('pendencies')
.snapshots();
@override
Widget build(BuildContext context) {
Size mediaQuery = MediaQuery.of(context).size;
return StreamBuilder<QuerySnapshot>(
stream: _mainStream,
builder: (context, mainSnapshot) {
if (mainSnapshot.hasError) {
return const Center(
child: Text('Something went wrong'),
);
}
if (mainSnapshot.connectionState == ConnectionState.waiting) {
return const Center(
child: CircularProgressIndicator(),
);
}
var pendenciesList = mainSnapshot.data!.docs;
print(pendenciesList);
return SafeArea(
child: SizedBox(
width: mediaQuery.width,
height: mediaQuery.height,
child: const Center(
child: Text('Test')
),
),
);
},
);
}
}
Currently, there are 2 documents in it.目前,其中有 2 个文档。 Is there a way to store the entire collection (documents and corresponded fields and values) inside a list?有没有办法将整个集合(文档和对应的字段和值)存储在列表中? If yes, how can I do it?如果是,我该怎么做?
I've tried var pendenciesList = mainSnapshot.data.;docs;
我试过var pendenciesList = mainSnapshot.data.;docs;
but got [Instance of '_JsonQueryDocumentSnapshot', Instance of '_JsonQueryDocumentSnapshot']
但得到[Instance of '_JsonQueryDocumentSnapshot', Instance of '_JsonQueryDocumentSnapshot']
yes you can do that是的,你可以这么做
//the list where you have all the data
List data = [];
...
// function to fill up the data list
getData() async {
await FirebaseFirestore.instance.collection("collectionName").get.then((value) {
for(var i in value.docs) {
data.add(i.data());
}
});
}
...
// get a field from a document in the data list
data[index]["field"];
The result of the snapshots()
function is of type Stream<QuerySnapshot<Map<String, dynamic>>>
meaning that it's like a stream of JSON values that you need to parse manually. snapshots()
function 的结果是Stream<QuerySnapshot<Map<String, dynamic>>>
类型,这意味着它就像 Z0ECD11C1D7A287401D148A23BBD7BZ 的 stream 一样,您需要手动解析值。
What you need to do is to define a function on your model object that can receive a value of type QueryDocumentSnapshot<Map<String, dynamic>> snapshot
and return a value of your model. What you need to do is to define a function on your model object that can receive a value of type QueryDocumentSnapshot<Map<String, dynamic>> snapshot
and return a value of your model. Here is an example:这是一个例子:
@immutable
class CloudNote {
final String documentId;
final String ownerUserId;
final String text;
const CloudNote({
required this.documentId,
required this.ownerUserId,
required this.text,
});
CloudNote.fromSnapshot(QueryDocumentSnapshot<Map<String, dynamic>> snapshot)
: documentId = snapshot.id,
ownerUserId = snapshot.data()[ownerUserIdFieldName],
text = snapshot.data()[textFieldName] as String;
}
Then when you retrieve your snapshots, you can convert them to your model object by mapping them as shown here:然后,当您检索快照时,您可以通过如下所示映射它们将它们转换为 model object:
Stream<Iterable<CloudNote>> allNotes({required String ownerUserId}) {
final allNotes = notes
.where(ownerUserIdFieldName, isEqualTo: ownerUserId)
.snapshots()
.map((event) => event.docs.map((doc) => CloudNote.fromSnapshot(doc)));
return allNotes;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.