Flutter中的窗口已满错误

时间:2018-06-25 17:48:13

标签: firebase dart google-cloud-firestore flutter

将数据从Firestore加载到列表视图中时,我收到此警告

  

W / CursorWindow(15035):窗口已满:请求分配483字节,可用空间274字节,窗口大小2097152字节

我正在寻求范围限定的模型模式,并在模型类中获取用户配置文件数据。我将所有用户数据保存到模型类的数组中,而不是在小部件树本身中使用streambuilder,以便更轻松地分页数据,并且坦率地更易于阅读。但是,在列表中,我收到一个“窗口已满”警告,我知道为存储用户配置文件的操作分配了过多空间,但是我可以采用其他方法来解决此问题吗?

class _ExploreScreenState extends State<ExploreScreen>
    with SingleTickerProviderStateMixin {

  Query _query;
  AnimationController controller;
  Animation<double> animation;
  int limitNum = 4;
  bool startAfter = false;
  User _lastUser;

  @override
  Widget build(BuildContext context) {
    return Container(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Expanded(
              child: new ScopedModel<ExploreModel>(
                  model: widget.model,
                  child: new ScopedModelDescendant<ExploreModel> (
                    builder: (context, child, model) =>
                    model.users.length != 0 ? GridView.builder(
                        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                            crossAxisCount: 2,
                            childAspectRatio: 0.8,
                            mainAxisSpacing: 5.0,
                            crossAxisSpacing: 5.0
                        ),
                        itemCount: model.users.length,
                        itemBuilder: (BuildContext context, int index) {
                          if(index ==  model.users.length-1){
                            model.loadUsers();
                          }
                          return BrowseTile(model.users[index]);
                        }
                    ) : Text('loading...'),
                  )
              ),
            ),
          ],
        )
    );
  }

class ExploreModel extends Model {
  List<User> _users;
  Query _query;
  User _currentUser;
  int limitNum = 20;


  List<User> get users => _users;

  ExploreModel(this._currentUser) {
    _users = new List();
    loadUsers();
  }

  void loadUsers() {
    _query = Firestore.instance.collection('Users').where(
        'gender', isEqualTo: _currentUser.prefGender)
        .limit(limitNum)
        .orderBy('firstName')
        .orderBy('lastName');
    if (_users.length > 0) _query = _query.startAfter(
        [_users[_users.length - 1].firstName, _users[_users.length - 1].lastName
        ]);
    print('Array Size: ');
    print(_users.length);
    _query.snapshots().listen((snapshot) {
      snapshot.documents.forEach((ds) {
        User user = User.fromMap(ds.data, ds.documentID);
        if(user.id != _currentUser.id){
          bool _added = false;
          _users.forEach((userEach) {
            for(var i=0; i<_users.length; i++){
              if(_users[i].id == user.id){
                _users.remove(_users[i]);
                _users.insert(i, user);
                _added = true;
                notifyListeners();
              }
            }
          });
          if (!_added) {
            _users.add(user);
            notifyListeners();
          }
        }
      });
    });
  }
}

0 个答案:

没有答案