flutter - 嵌套流构建器以从云 Firestore 获取数据
问题描述
我有显示我正在关注的用户帖子的主页,我想从 Firestore 获取我正在关注的用户的 uid,然后转到用户集合然后发布集合并显示最新的两个帖子。
第一步,我们必须转到以下集合,然后获取每个文档的 uid。
第二步,我们必须转到用户集合,然后其中 uid 等于我们在第一步中获得的每个 uid,然后在帖子中获取最新的 2 个两个文档(按“日期”排序)。
我已尝试此代码但无法正常工作,并且出现错误:
════════ (546) Exception caught by widgets library ═════════════════════════════════════════════════
RangeError (index): Invalid value: Only valid value is 0: 4
The relevant error-causing widget was:
StreamBuilder<QuerySnapshot> file:///Users/Mr.3bd/AndroidStudioProjects/app/test/lib/Screens/HomePage.dart:351:48
════════════════════════════════════════════════════════════════════════════════════════════════════
我的代码:
StreamBuilder(
stream: _firestore
.collection(FOLLOW_COLLECTION)
.document(widget.myuid)
.collection(FOLLOWING_COLLECTION)
.snapshots(),
builder: (context, fir_snapshot) {
if (!fir_snapshot.hasData) {
return Center(child: SpinKitThreeBounce(
color: Colors.white,
size: 50.0,));
}
else if(fir_snapshot.hasError)
{
return Center(child: Text("No Data",style: TextStyle(color: GlobalUniversal.purple),));
} else {
return ListView.builder(
physics:
NeverScrollableScrollPhysics(),
shrinkWrap: true,
scrollDirection: Axis.vertical,
itemCount: fir_snapshot
.data.documents.length,
itemBuilder: (context, index) {
DocumentSnapshot myfollowing =
fir_snapshot.data
.documents[index];
return StreamBuilder(
stream: _firestore
.collection(
USERS_COLLECTION)
.where('uid',
isEqualTo:
myfollowing['uid'])
.snapshots(),
builder:
(context, sec_snapshot) {
if (!sec_snapshot.hasData) {
return Center(child: SpinKitThreeBounce(
color: Colors.white,
size: 50.0,));
}
else if(sec_snapshot.hasError)
{
return Center(child: Text("No Data",style: TextStyle(color: GlobalUniversal.purple),));
}
else {
DocumentSnapshot user =
sec_snapshot.data
.documents[index];
return StreamBuilder(
stream: _firestore
.collection(
USERS_COLLECTION)
.document(user.documentID)
.collection(POSTS_COLLECTION)
.orderBy('date', descending: true)
.snapshots(),
builder:
(context, thir_snapshot) {
if (!thir_snapshot.hasData) {
return Center(child: SpinKitThreeBounce(
color: Colors.white,
size: 50.0,));
}
else if(thir_snapshot.hasError)
{
return Center(child: Text("No Data",style: TextStyle(color: GlobalUniversal.purple),));
}
else {
DocumentSnapshot post =
thir_snapshot.data
.documents[index];
int usefulCount = post['Usefuls'];
int likeCount = post['Likes'];
int commentCount = post['Comments'];
return Padding(
padding:
EdgeInsets.only(
top: 10.0,
bottom:
10.0),
child: InkWell(
onTap :(){
// Navigator.push(context, new MaterialPageRoute(builder: (context) => new Comments(myapplang: widget.myapplang,myuid: widget.myuid,myfullname: widget.fullname,myimg: widget.uprofilePicUrl,CommentMajCount: widget.CommentMajCount,pid: mypost['pid'], uid: mypost['uid'], majname: widget.majname, pdoc: mypost.documentID.toString())));
},
child: Container(
decoration:
BoxDecoration(
color: GlobalUniversal
.whiteBG,
),
child: Padding(
padding:
EdgeInsets
.all(
8.0),
child: Column(
children: <
Widget>[
SizedBox(
height:
10.0,
),
Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
crossAxisAlignment:
CrossAxisAlignment.center,
children: <
Widget>[
Padding(
padding: widget.myapplang == 'ar' ? EdgeInsets.only(right: 5.0) : EdgeInsets.only(left: 5.0),
child: Row(
mainAxisAlignment:
MainAxisAlignment.center,
crossAxisAlignment:
CrossAxisAlignment.center,
children: <Widget>[
Container(width: 35.0, height: 35.0, decoration: BoxDecoration(shape: BoxShape.circle, image: DecorationImage(fit: BoxFit.fill, image: NetworkImage(
// user image url
user['uimg'])))),
SizedBox(
width: 10.0,
),
Text(
'${user['name']}',
style: TextStyle(color: GlobalUniversal.blackColor, fontSize: 14.0, fontWeight: FontWeight.bold, fontFamily: 'BalooDa2'),
),
],
),
),
IconButton(
icon: Icon(
Icons.menu,
color: GlobalUniversal.blackColor,
),
onPressed: () {
})
],
),
SizedBox(
height:
20.0,
),
Padding(
padding:
EdgeInsets.all(5.0),
child: Text(
'${post['text']}',
style: TextStyle(
color: GlobalUniversal.blackColor,
fontSize: 16.0,
fontFamily: 'Tajawal',
height: 1.3),
textAlign: TextAlign.justify,
textDirection: TextDirection.rtl),
),
SizedBox(
height:
15.0,
),
Visibility(
child:
Text(
'${post['pid']}',
),
visible:
false,
),
Visibility(
visible:
_isVisible,
child:
ClipRRect(
borderRadius:
BorderRadius.circular(8.0),
child:
Image.network(
'${post['img']}' != null
? '${post['img']}'
: showToast,
fit:
BoxFit.fill,
),
),
),
SizedBox(
height:
15.0,
),
Divider(),
Container(
width: 180,
height: 40,
child: RaisedButton(
child: Text(widget.myapplang == 'ar' ? "أكتب تعليق.." : "Write a comment..", style: TextStyle(color: GlobalUniversal.blackColor.withOpacity(0.5), fontSize: 12, fontWeight: FontWeight.bold, fontFamily: widget.myapplang == 'ar' ? 'Tajawal' : 'BalooDa2')),
elevation: 0.0,
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(25.0),
),
color: GlobalUniversal.secondWhiteBG,
onPressed: () async {
// Navigator.push(context, new MaterialPageRoute(builder: (context) => new Comments(myapplang: widget.myapplang, myuid: widget.myuid, myfullname: widget.fullname, myimg: widget.uprofilePicUrl, CommentMajCount: widget.CommentMajCount, pid: mypost['pid'],uid: mypost['uid'], majname: widget.majname, pdoc: mypost.documentID.toString())));
},
),
),
],
),
),
),
),
);
}
},
);
}
},
);
});
}
return Container(
height: 0.0,
width: 0.0,
);
},
),
解决方案
推荐阅读
- c# - 如何在特定计时器后更改每个动态创建的标签的背景色?c#
- css - 尽管有块值,但元素没有从新行开始?
- regex - 使用正则表达式获取高度和长度
- javascript - 登录后设置不记名令牌
- php - 在php中重定向到Iframe之外的另一个页面
- ansible - 从 ansible playbook 内部执行 docker-compose
- sql - 使用 DAX 在 Power BI 中分离列
- javascript - 如何正确地将“this”对象发送到函数
- firebase - Firebase 实时数据库中有关身份验证的信息
- flask - 烧瓶护身符。将sha256列入白名单不起作用