【问题标题】:Why my widget is not returning the else block?为什么我的小部件没有返回 else 块?
【发布时间】: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


【解决方案1】:

uid 字段不是数组,但您使用arrayContains 来查询它。那是行不通的。如果要检查单值字段是否具有多个值之一,请使用in operation

.where('uid', whereIn: listOfIds)

【讨论】:

  • 嘿,坦率地说,我正在使用它,但我遇到了 2 个问题,第一个问题是当 listofids 为空时它只是在加载。另一个问题是,在我的情况下,我无法弄清楚如何解决这个问题,当我更新问题和帖子时,你可能会看看这个问题吗?
  • 我不确定第一部分是什么意思。您的 arrayContains 将不起作用,因为 uid 字段不是数组。不管出了什么问题,您都需要使用whereIn 来检查uid 字段与listOfIds 的值。
  • 对不起,也许你理解错了,当我使用 where 并且列表为空而不给出 else(默认文本)时,让我们解决第一个问题。它只是加载并且永不停止。当 listofids 不为空时,它会起作用。
  • 我更新了问题,以便您可以查看
  • 我对您更新的问题发表了评论。
猜你喜欢
  • 2020-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多