【发布时间】:2021-11-02 03:24:53
【问题描述】:
我的 FutureBuild 在我的 ListView 中,因此我无法访问文档长度。有什么办法吗?我想制作一个 PostsContainer 列表,我知道这有点乱,但我没有找到其他方法。
这是我的代码:
Future getPosts() async {
var firestore = FirebaseFirestore.instance;
qn = await firestore.collection("Posts").get();
qn.docs;
count = qn.docs.length;
}
return Scaffold(
backgroundColor: Colors.black,
body: CustomScrollView(
slivers: <Widget>[
appBar,
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) {
return FutureBuilder(
future: getPosts(),
builder: (_, snapshot) {
if(snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: Text('Loading', style: TextStyle(color: Colors.white),),
);
}
else {
return PostContainer(
post: Post(user: UserModel(
imageUrl: 'https://images.ctfassets.net/hrltx12pl8hq/7yQR5uJhwEkRfjwMFJ7bUK/dc52a0913e8ff8b5c276177890eb0129/offset_comp_772626-opt.jpg?fit=fill&w=800&h=300',
name: qn.docs[index].get('name'),
),
caption: qn.docs[index].get('Text') != null ? qn.docs[index].get('Text') : null,
timeAgo: '0 min',
imageUrl: qn.docs[index].get('img') != null ? qn.docs[index].get('img') : null,
likes: 6345,
comments: 15,
shares: 18,
views: 45
),
);
}
}
);
},
childCount: count,
)
)
],
),
);```
Thanks in advance!
【问题讨论】:
标签: flutter flutter-futurebuilder