My FutureBuild is in my ListView and because of that i cant acces the document length. Is there any way to do it? I wanna make a List of PostsContainer i know it's a little messy but i found no other way.
Here is my Code:
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!
I got it working! Here is my script.
QuerySnapshot qn;
Future getPosts() async {
var firestore = FirebaseFirestore.instance;
qn = await firestore.collection("Posts").get();
qn.docs;
}
return FutureBuilder(
future: getPosts(),
builder: (context, snapshot){
Widget newsListSliver;
if(snapshot.connectionState == ConnectionState.waiting) {
newsListSliver = SliverToBoxAdapter(child: CircularProgressIndicator(),);
} else {
newsListSliver = SliverList(delegate: SliverChildBuilderDelegate((context, index) {
return PostContainer(
post: Post(
user: UserModel(
imageUrl: qn.docs[index].get('picture'),
name: qn.docs[index].get('name'),
),
caption: qn.docs[index].get('Caption') != null ? qn.docs[index].get('Caption') : null,
text: 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: qn.docs.length,)
);
}
return CustomScrollView(
slivers: <Widget>[
appBar,
newsListSliver
],
);
}, );