firebase - Error when getting data from StreamBuilder BloC firestore Flutter
问题描述
I have been having problems with my APP in Flutter... I tried to get data from Bloc and Repositore and Firestore and it displays in my GridView, but get this error:
My UI Code:
Widget bodyGallery2() => SafeArea(
child: Scaffold(
body: Stack(
children: <Widget>[
StreamBuilder<List<PhotoDish>>(
stream: _dishBloc.streamPhotoDish,
builder: (context, snapshot) {
if (!snapshot.hasData) {
return CircularProgressIndicator();
}
print(snapshot.toString());
return GridView.count(
crossAxisCount: 1,
crossAxisSpacing: 4.0,
mainAxisSpacing: 8.0,
childAspectRatio: 3/2,
children: snapshot.data
.map((photo)=>buildItemPhoto(photo)),
);
}
)
]
),
),
);
Widget buildItemPhoto(PhotoDish listphoto){
return Text(listphoto.idDish+":::PhotoDish");
}
My Bloc Code, it has the Stream to list of PhotoDish from my Repo:
class DishBloc extends Bloc{
RepositoryFirestore _repository = RepositoryFirestore();
PhotoDishRepo _repo = FirestoreDishProvider();
Stream<List<PhotoDish>> get streamPhotoDish => _repo.streamPhotoDish;
My Repo Code has the call to get data from firebase and it set in StreamController:
abstract class PhotoDishRepo {
Stream<List<PhotoDish>> get streamPhotoDish;
}
class FirestoreDishProvider implements PhotoDishRepo{
StreamController<List<PhotoDish>> _streamController = BehaviorSubject<List<PhotoDish>>();
Firestore _firestore = Firestore.instance;
List<PhotoDish> _listPhotoDish = List();
FirestoreDishProvider() {
_firestore
.collection(Constants.namePhotoDishCollection)
.snapshots()
.listen((QuerySnapshot snapshot){
snapshot.documents.forEach((obj){
_listPhotoDish.add(PhotoDish(
date: DateTime.now(),
id: obj.data["id"],
idDish: obj.data["idDish"],
idUser: obj.data["idUser"],
photo: obj.data["photo"],
));
});
print("TOTALL:::"+_listPhotoDish.length.toString());
_streamController.add(_listPhotoDish);
});
}
解决方案
I think you are just miss to add .ToList() method at the end where you are mapping list into widget.
Map only convert your data into widget, which return single widget while gridview children argument require list of widgets, so you have to covert it into list.
children: snapshot.data
.map((photo)=>buildItemPhoto(photo)).toList(), //convert to list
);
推荐阅读
- ios - react-native run-ios 不断失败
- google-bigquery - 我已通过 API 密钥将外部方链接到我的 Google BigQuery 实例。我如何控制他们能看到多少我的数据?
- indexeddb - 存储在 IndexedDB 中的最大记录数是多少
- python - 我如何将 django 中的多个图像上传到网站上文章的不同部分?
- javascript - 高效地将 JavaScript 键和值数组转换为对象
- elasticsearch - Elasticsearch:使用数组搜索包含数组的字段
- clojurescript - 试剂 - 将 ui 状态与(或分开?)服务器持久状态一起存储的方法?
- python - 从 Python 中的 iso 日期时间戳中提取星期几和小时
- python - Python中与列的复杂分离
- sql-server - 第一次运行每晚重新编译的存储过程非常慢,然后快得多