有没有办法将 SliverList 的子计数设置为 Firebase 集合长度?
Is there a way to set the childcount of a SliverList to the Firebase Collection Length?
我的 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!
试试这个
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,
FutureBuilder(
future: getPosts(),
builder: (_, snapshot) {
if(snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: Text('Loading', style: TextStyle(color: Colors.white),),
);
}
else {
return SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) {
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,
)
);
}
}
)
],
),
);
您只是按错误的顺序包装了小部件。
正确的是 Sliverlist 应该在 future builder
里面
我成功了!这是我的脚本。
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
],
);
},
);
我的 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!
试试这个
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,
FutureBuilder(
future: getPosts(),
builder: (_, snapshot) {
if(snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: Text('Loading', style: TextStyle(color: Colors.white),),
);
}
else {
return SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) {
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,
)
);
}
}
)
],
),
);
您只是按错误的顺序包装了小部件。 正确的是 Sliverlist 应该在 future builder
里面我成功了!这是我的脚本。
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
],
);
}, );