【问题标题】:Why my method give me only data if im hardcoding the id?如果我对 id 进行硬编码,为什么我的方法只给我数据?
【发布时间】:2021-07-15 00:06:01
【问题描述】:

我正在尝试从 Firebase 获取数据。但我不知道为什么它不起作用。 首先是我的代码

  Stream myVideos;

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 = await FirebaseFirestore.instance
        .collection('videos')
        .where( 'uid',arrayContains: listOfIds)
        .snapshots();
    var documents = await FirebaseFirestore.instance
        .collection('videos')
        .where( 'uid',arrayContains: listOfIds)
        .get();
        print(documents.docs.length);

所以它应该工作,但它没有。当打印给我这个时,ids 列表不为空

flutter: [Fp3unLwcl2SGVh4MbUPiRVAylYV2]

那是绝对正确的。但是 id 不起作用。当我输入而不是 listofIds 时,id

Fp3unLwcl2SGVh4MbUPiRVAylYV2 

在每个 arrayContains 之后它都能完美运行。那么……这里发生了什么。我真的疯了。打印文档.docs.length 给我 0 但为什么? 这就是我使用它的方式


  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());
                            }

                            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: () {
                                      NavigationService.instance
                                          .navigateToRoute(MaterialPageRoute(
                                              builder: (context) {
                                        return VideoPage(
                                          video.data()['videourl'],
                                          video.data()['uid'],
                                          video.id,
                                        );
                                      }));
                                    },
                                    child: Card(
                                      elevation: 0.0,
                                      child: ClipRRect(
                                        borderRadius: BorderRadius.circular(25),
                                        clipBehavior:
                                            Clip.antiAliasWithSaveLayer,
                                        child: Image.network(
                                          video.data()['previewimage'],
                                          fit: BoxFit.cover,
                                        ),
                                      ),

                                      //imageData: searchImages[index],
                                    ),
                                  );
                                },
                                staggeredTileBuilder: (index) =>
                                    StaggeredTile.count(
                                        (index % 7 == 0) ? 2 : 1,
                                        (index % 7 == 0) ? 2 : 1),
                                mainAxisSpacing: 8.0,
                                crossAxisSpacing: 4.0,
                              );
                            } 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),
                                    ),
                                  ),
                                ),
                              );
                            }
                          }),
                    ]),
                  ],
                ),
              ),
            ),
          ]);
  }

我只是一次又一次地得到 else 块。但是,如果我悲伤地设置更改每个 where 块来自

 .where( 'uid',arrayContains: listOfIds). 

到这里

 .where( 'uid',arrayContains: 'Fp3unLwcl2SGVh4MbUPiRVAylYV2') 

它有效,但为什么。 id 列表再次给了我这个

 flutter: [Fp3unLwcl2SGVh4MbUPiRVAylYV2] 

。同一个id。

【问题讨论】:

    标签: firebase flutter dart firebase-realtime-database google-cloud-firestore


    【解决方案1】:

    请改用.where('uid', in: listOfIds)

    Array-contains: 用于检查 uid 是否为包含项目的数组。 In: 用于获取属性在指定数组中的文档

    【讨论】:

    • 我试了一下,它说'in'不能用作标识符,因为它是一个关键字。尝试将其重命名为不是关键字的标识符
    • 我用 whereIn that works 兄弟 100 次谢谢
    • 我被困在这他妈的 3 天不敢相信
    • 命名肯定是混乱的。很高兴你解决了!
    • 我有另一个关于这种类型的问题我会打开一个新问题也许你可以看看它并节省我的时间
    最近更新 更多