我有一个FutureBuilder(非常适合加载)从Firebase获取我的DocumentSnapshot列表,但是问题是,如果我按下/弹出屏幕,它将一遍又一遍地重建它。为了解决这个问题,我终于将其放置在initState
上,但是有一个问题,有时我并没有从中获取所有文档。
我有一个启用和禁用国家/地区过滤器的按钮,每次按下该按钮时,几乎都会立即显示我的文档,例如,我总共过滤了11个文档,过滤了5个文档,但是有时我并没有全部获取它们,直到获得我得到的值应用错误(_userList
的长度?)。我试图放置一个Future来等待,但是我无法执行,请告知。
var _userList;
_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:
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,
)
按钮:
onPressed: () async {
if (_countryFilter) {
_countryFilter = false;
} else {
_countryFilter = true;
}
await _fetchUserList();
setState(() {});
},
答案 0 :(得分:0)
我已经设法通过一些验证使其工作。同样,用户长度不同的问题是因为在buildItem上也使用了“ country”变量。
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(() {});
}
}
(_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和Button上。