6

When loading data from Firestore into a listview, i receive this warning

W/CursorWindow(15035): Window is full: requested allocation 483 bytes, free space 274 bytes, window size 2097152 bytes

I am suing scoped model pattern and getting user profile data in the model class. I saved all the user data into an array in the model class instead of using an streambuilder in the widget tree itself so that it is easier to page through the data, and it's frankly easier to read. However, going through the list, i receive a Window is Full warning, i understand that too much space is being allocated for this operation of storing the user profiles, however is there an alternative approach i can take to this problem?

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();
          }
        }
      });
    });
  }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
GAvi
  • 69
  • 1
  • 4
  • possibly related: https://stackoverflow.com/questions/35266874/getting-heavy-data-throws-cursor-window-window-is-full-error/35267159 – Jacob Phillips Jun 25 '18 at 17:58

1 Answers1

1

This seems to be a known issue as mentioned in this GitHub issue ticket, but this should have been resolved as of May 2020. I suggest upgrading the Flutter SDK version that you're using and try it again.

Omatt
  • 8,564
  • 2
  • 42
  • 144