Flutter-使用FutureBuilder从Firestore获取数组(使用查询) [英] Flutter - Get array from Firestore with FutureBuilder (using query)
问题描述
我在这里非常困惑……如果我需要执行文档(在哪里)查询,使用FutureBuilder从Firestore中列出数组的正确语法是什么?
I'm confusing myself terribly here...what is the correct syntax for listing an array from Firestore with FutureBuilder if I need to perform a document (where) query?
Firestore数据库结构:
- [companies]
- [doc]
- [gallery]
- [doc]
- [reference] <- (ie, companies/doc)
- [gallery_images] // List these, but show only images that match where() query
这是我当前的代码:
Future<List<dynamic>> getGallery() async {
var firestore = Firestore.instance;
DocumentReference docRef = firestore.collection('companies').document(widget.docID);
var ref = firestore.collection('galleries').where('companyRef', isEqualTo: docRef).getDocuments();
return ref.get().then((datasnapshot) {
if (datasnapshot.exists) {
List<dynamic> imageArray = datasnapshot.data['gallery_images'].toList();
return imageArray;
} else {
print("Please just work...");
}
},
);
}
FutureBuilder(
future: getGallery(),
builder: (context, AsyncSnapshot<List<dynamic>> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: Text("Loading..."),
);
} else {
return GridView.builder(
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
itemCount: *length*,
itemBuilder: (BuildContext context, int index) {
return ViewGalleryItemThumbnail(
viewGalleryItem: snapshot.data[index],
....
如果有人可以告诉我我要去哪里错了,我将不胜感激!
Would really appreciate if someone can show me where I'm going wrong!
推荐答案
目前尚不清楚Cloud Firestore模式的外观,但这看起来更有可能返回正确的动态列表. (请注意,通过将泛型添加到地图,您可以返回具体想要的列表-例如.map<SomeRealType>((snap) => snap.data['gallery_images'])
.)
It's not exactly clear what your Cloud Firestore schema looks like, but this looks more likely to return you the correct list of dynamics. (Note that by adding a generic to the map you could return a list of specifically what you want - e.g. .map<SomeRealType>((snap) => snap.data['gallery_images'])
.)
当您受困于FutureBuilder
时,暂时恢复为有状态的小部件有时会很有用.覆盖initState
,并调用经过稍微修改的getGallery
,该末尾不会返回任何值,而是将其分配给setState
内部的成员变量.这样,打印语句的机会就很多.另外,尝试将异步函数中的任何.then
转换为线性的await
系列.它使读取变得更容易(并且调试打印以合理的线性顺序出现).
When you're stuck with FutureBuilder
it's sometimes useful to revert temporarily to a stateful widget. Override initState
, and call a slightly modified getGallery
which doesn't return a value at the end, but assigns it to a member variable inside setState
. Plenty of opportunity for print statements that way. Additionally, try to convert any .then
s in an async function into a linear series of await
s. It makes it easier to read (and your debug prints come out in a linear sensible order).
Future<List<dynamic>> getGallery() async {
var firestore = Firestore.instance;
var docRef = firestore.collection('companies').document(widget.docID);
var querySnap = await firestore
.collection('galleries')
.where('companyRef', isEqualTo: docRef)
.getDocuments();
return querySnap.documents
.map((snap) => snap.data['gallery_images'])
.toList();
}
这篇关于Flutter-使用FutureBuilder从Firestore获取数组(使用查询)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!