首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >颤振List<DocumentSnapshot>没有得到所有的值

颤振List<DocumentSnapshot>没有得到所有的值
EN

Stack Overflow用户
提问于 2019-08-06 16:25:36
回答 1查看 1.8K关注 0票数 0

我有一个FutureBuilder (非常适合加载),从Firebase获得了我的DocumentSnapshot列表,但问题是,如果我按/弹出一个屏幕,它就会一次又一次地重新构建它。为了解决这个问题,我最终把它放到了initState上,但我有一个问题,有时我不会从它得到所有的文档。

我有一个按钮来启用国家过滤器和禁用,每次我按它几乎立即显示我的文件,例如,我有11个总数和5个过滤,但我没有得到他们全部有时,直到我得到的值,我得到的应用程序错误(长度的_userList?)。我试图把未来的力量等待,但我不能,请告知。

var _userList;

代码语言:javascript
复制
_fetchUserList() {
    Future<QuerySnapshot> snapshot;
    if (_countryFilter) {
      //print('COUNTRY FILTER');
      snapshot = Firestore.instance.collection('u').where('country', isEqualTo: country).getDocuments();
    } else {
      //print('ALL FILTER');
      snapshot = Firestore.instance.collection('u').getDocuments();
    }
    snapshot.then((QuerySnapshot result) {
      final List<DocumentSnapshot> documents = result.documents;
      if (documents.length > 0) {
        final List<DocumentSnapshot> availableUsers =
            documents.where((DocumentSnapshot documentSnapshot) => documentSnapshot['userId'] != _id).toList();
        //print('userList Size: ' + availableUsers.length.toString());

        List<DocumentSnapshot> userShuffle = _shuffle(availableUsers);
        setState(() {
          _userList = userShuffle;
        });
      }
    });
  }

这是我的GridView:

代码语言:javascript
复制
GridView.builder(
                    padding: const EdgeInsets.all(10.0),
                    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 2, mainAxisSpacing: 10.0, crossAxisSpacing: 10.0, childAspectRatio: 0.85),
                    itemBuilder: (context, index) {
                      return _buildItem(context, _userList[index]);
                    },
                    itemCount: _userList.length,
                  )

按钮:

代码语言:javascript
复制
onPressed: () async {
            if (_countryFilter) {
              _countryFilter = false;
            } else {
              _countryFilter = true;
            }
            await _fetchUserList();
            setState(() {});
          },
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-08 13:39:15

我已经设法使它工作使用一些验证。另外,不同用户长度的问题也是因为在buildItem上使用了"country“变量。

代码语言:javascript
复制
Future _fetchUserList() async {
    setState(() {
      _userList = null;
      _userLenght = null;
    });
    QuerySnapshot snapshot;
    if (_countryFilter) {
      snapshot = await Firestore.instance.collection('u').where('country', isEqualTo: _myCountry).getDocuments();
    } else {
      snapshot = await Firestore.instance.collection('u').getDocuments();
    }

    final List<DocumentSnapshot> documents = snapshot.documents;
    if (documents.length > 0) {
      final List<DocumentSnapshot> availableUsers =
          documents.where((DocumentSnapshot documentSnapshot) => documentSnapshot['userId'] != _id).toList();
      _userLenght = availableUsers.length;

      if (_userLenght > 0) {
        List<DocumentSnapshot> userShuffle = _shuffle(availableUsers);
        _userList = userShuffle;
      }
      setState(() {});
    }
  }
代码语言:javascript
复制
(_userList == null && _userLenght == null)
                      ? Container(
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.stretch,
                            children: <Widget>[
                              Center(
                                child: Container(
                                  width: 50.0,
                                  height: 50.0,
                                  alignment: Alignment.center,
                                  child: CircularProgressIndicator(
                                    valueColor: AlwaysStoppedAnimation<Color>(Theme.of(context).primaryColor),
                                    strokeWidth: 2.0,
                                  ),
                                ),
                              ),
                              SizedBox(height: 12.0),
                              Text(
                                "We are loading the users",
                                style: TextStyle(
                                  color: Colors.black54,
                                  fontWeight: FontWeight.bold,
                                ),
                                textAlign: TextAlign.center,
                              ),
                            ],
                          ),
                        )
                      : (_userLenght == 0)
                          ? Container(
                              child: Column(
                                mainAxisAlignment: MainAxisAlignment.center,
                                crossAxisAlignment: CrossAxisAlignment.stretch,
                                children: <Widget>[
                                  Center(
                                    child: Icon(
                                      Icons.supervisor_account,
                                      color: Theme.of(context).primaryColor.withOpacity(0.8),
                                      size: 50.0,
                                    ),
                                  ),
                                  SizedBox(height: 12.0),
                                  Text(
                                    "No one was found\nTry reloading the screen",
                                    style: TextStyle(
                                      color: Colors.black54,
                                      fontWeight: FontWeight.bold,
                                    ),
                                    textAlign: TextAlign.center,
                                  ),
                                ],
                              ),
                            )
                          : GridView.builder(
                              padding: const EdgeInsets.all(10.0),
                              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                                  crossAxisCount: 2,
                                  mainAxisSpacing: 10.0,
                                  crossAxisSpacing: 10.0,
                                  childAspectRatio: 0.85),
                              itemBuilder: (context, index) {
                                return _buildItem(context, _userList[index]);
                              },
                              itemCount: _userLenght,
                            ),

希望这能帮上忙。有了这个,我能够停止屏幕用户列表的洗牌每次我推和弹出一个屏幕,因为它只在initState和按钮上。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57380352

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档