[英]Flutter How to pass in documents.length from Firestore in another method?
I have a method which takes an int
value, which is supposed to be the value of the length of an array
field in a document in a collection in Cloud Firestore .我有一个采用
int
值的方法,该值应该是Cloud Firestore集合中文档中array
字段长度的值。
I'm very new to both Flutter and especially Firestore, and I figure it has something to do with how futures, queries and streams works, but I can't seem to understand it correctly.我对 Flutter 尤其是 Firestore 都很陌生,我认为这与期货、查询和流的工作方式有关,但我似乎无法正确理解它。
This is what I'm trying to achieve:这就是我想要实现的目标:
class UserBookmarksSection extends StatefulWidget {
@override
_UserBookmarksSectionState createState() => _UserBookmarksSectionState();
}
class _UserBookmarksSectionState extends State<UserBookmarksSection> {
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
HeadlineItem(title: "Your bookmarks"),
StreamBuilder(
stream: Firestore.instance.collection("brites").snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) return const Text("Loading...");
return Column(
children: populateBriteList(snapshot, x, true), //Here I want to pass in the length of the array in firestore into x
);
}
),
],
);
}
}
Basically I've tried to create a method which returns a Future<int>
and tried to pass that, but that didn't work because a Future<int>
isn't the same type as int
.基本上,我尝试创建一个返回
Future<int>
的方法并尝试传递它,但这不起作用,因为Future<int>
与int
类型不同。 That I understand, but the solution is not clear to me.我明白,但解决方案对我来说并不清楚。
I'm not sure the relationship between brites and bookmarks- that is, which you expect to change more often.我不确定 brites 和书签之间的关系——也就是说,你希望更频繁地改变它。 I'm going to assume that the bookmarks isn't changing once the user opens the bookmarks section.
我将假设一旦用户打开书签部分,书签就不会改变。
If that is the case you can retrieve the bookmarks length value in initState.如果是这种情况,您可以在 initState 中检索书签长度值。 You will override the state class's initState() method, and then call an async method which retrieves the value from the database.
您将覆盖 state 类的 initState() 方法,然后调用从数据库中检索值的异步方法。 (init state can't be async itself).
(init state 本身不能是异步的)。 Once the value is retrieved, you can then call setState() to update the widget with the value of bookmarks set as an int.
检索到值后,您可以调用 setState() 以使用设置为 int 的书签值更新小部件。
Might look something like this:可能看起来像这样:
class UserBookmarksSection extends StatefulWidget {
@override
_UserBookmarksSectionState createState() => _UserBookmarksSectionState();
}
class _UserBookmarksSectionState extends State<UserBookmarksSection> {
int bookmarksLength;
@override
void initState(){
super.initState();
getBookmarksLength();
}
Future<void> getBookmarksLength() async {
bookmarksLength = await getArrayLength(id);
setState((){});
}
@override
Widget build(BuildContext context) {
if(bookmarksLength == null) return CircularProgressIndicator();
else return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
HeadlineItem(title: "Your bookmarks"),
StreamBuilder(
stream: Firestore.instance.collection("brites").snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) return const Text("Loading...");
return Column(
children: populateBriteList(snapshot, bookmarksLength, true),
);
}
),
],
);
}
}
Firestore firestore = Firestore.instance;
Future<int> getArrayLength(String documentId) async {
DocumentSnapshot snapshot = await firestore.collection('collection').document(documentId).get();
return snapshot.data['array'].length;
}
A more robust solution might be to have a separate class that handles both listening to the stream and retrieving the length of the array, and then combining all of the information you're going to display into some abstract data type which you put into a separate stream you specify that is specifically for the UI to listen to.一个更强大的解决方案可能是拥有一个单独的 class 来处理监听 stream 和检索数组的长度,然后将您要显示的所有信息组合成一些抽象数据类型,您将其放入单独的您指定的 stream 是专门供 UI 监听的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.