【发布时间】:2021-05-04 13:00:13
【问题描述】:
为什么我的列表为空时会出现此错误?
[VERBOSE-2:ui_dart_state.cc(186)] Unhandled Exception: 'package:cloud_firestore/src/query.dart': Failed assertion: line 435 pos 16: '(value as List).isNotEmpty': 'in' filters require a non-empty [List].
#0 _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:46:39)
#1 _AssertionError._throwNew (dart:core-patch/errors_patch.dart:36:5)
#2 Query.where
package:cloud_firestore/src/query.dart:435
#3 _MeineFreundeState.getalldata
package:wichtigdenyady/seitenleiste/meinefreunde.dart:48
<asynchronous suspension>
这是我的代码
Widget getBody(BuildContext context) {
return dataisthere == false
? Scaffold(body: Center(child: CircularProgressIndicator()))
: Stack(children: <Widget>[
Scaffold(
appBar: AppBar(
actions: [
IconButton(
icon: Icon(Icons.search),
onPressed: () {
Navigator.of(context)
.pushNamed(Searchuserinmeinebeitraege.route);
},
),
],
backgroundColor: Colors.transparent,
elevation: 0.0,
),
body: RefreshIndicator(
onRefresh: _handleRefresh,
color: Colors.black,
strokeWidth: 4,
child: ListView(
children: [
Column(children: <Widget>[
SizedBox(
height: 5,
),
StreamBuilder(
stream: myVideos,
builder: (context, snapshot) {
if (snapshot.connectionState ==
ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
}else { if (videos > 0) {
return StaggeredGridView.countBuilder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
physics: ScrollPhysics(),
crossAxisCount: 3,
itemCount: snapshot.data.docs.length,
itemBuilder: (context, index) {
DocumentSnapshot video =
snapshot.data.docs[index];
return InkWell(
onTap: () {
currentvideoindex = index;
NavigationService.instance
.navigateToRoute(MaterialPageRoute(
builder: (context) {
return MeineFreundeVideos(
video.data()['videourl'],
video.data()['uid'],
video.id,
currentvideoindex,
listofeachid);
}));
},
child: Card(
elevation: 0.0,
child: ClipRRect(
borderRadius: BorderRadius.circular(25),
clipBehavior:
Clip.antiAliasWithSaveLayer,
child: Image.network(
video.data()['previewimage'],
fit: BoxFit.cover,
),
),
),
);
},
staggeredTileBuilder: (index) =>
...
);
} else {
return Center(
child: Padding(
padding:
const EdgeInsets.fromLTRB(0, 100, 0, 0),
child: Container(
child: Text(
"No Videos Yet",
style: TextStyle(
fontSize: 18, color: Colors.black),
),
这是我定义列表的地方
getalldata() async {
List listOfIds = [];
String myID = FirebaseAuth.instance.currentUser.uid;
var idofotheruser = await FirebaseFirestore.instance
.collection('meinprofilsettings')
.doc(myID)
.collection('following')
.get();
following = idofotheruser.docs.length;
idofotheruser.docs.forEach((element) {
listOfIds.add(element.id);
});
print(listOfIds);
myVideos = FirebaseFirestore.instance
.collection('videos')
.where('uid', whereIn:listOfIds)
.snapshots();
var documents = await FirebaseFirestore.instance
.collection('videos')
.where('uid', whereIn: listOfIds)
.get();
setState(() {
videos = documents.docs.length;
print(videos);
});
因此,当列表为空且未停止时,它的加载(返回循环进度指示器)。当它不是那么它的工作原理。希望任何人都可以提供帮助。如果您需要更多信息,请发表评论。如果您需要更多代码,请发表评论
【问题讨论】:
-
将空列表传递给
whereIn似乎无效。当listOfIds为空时,您期望myVideos的结果是什么? -
当您查看 getalldata 方法时,我设置了一个 int videos 等于文档的长度。然后我检查它是否大于 0,然后显示所有视频,如果没有返回文本,则没有视频
-
我也更新了我的代码,也许它不太好理解
-
谢谢。但这不是我问题的答案。你得到的错误是
'in' filters require a non-empty [List],因为你的listOfIds是空的。当列表为空时,您希望myVideos和/或documents是什么? -
if (listOfIds.isEmpty) { myVideos = [] } else {myVideos = FirebaseFirestore.instance .collection('videos') .where('uid', whereIn:listOfIds) .snapshots(); }?
标签: firebase flutter dart google-cloud-firestore