在Flutter中,我的APP一直存在问题...我试图从Bloc,Repositore和Firestore获取数据,并且该数据显示在GridView中,但出现此错误:
我的用户界面代码:
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");
}
我的Bloc代码,它具有从我的Repo到PhotoDish列表的流:
class DishBloc extends Bloc{
RepositoryFirestore _repository = RepositoryFirestore();
PhotoDishRepo _repo = FirestoreDishProvider();
Stream<List<PhotoDish>> get streamPhotoDish => _repo.streamPhotoDish;
我的Repo代码具有从Firebase获取数据的调用,并且已在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);
});
}
答案 0 :(得分:0)
我认为您只是想在将列表映射到小部件的末尾添加.ToList()方法。
地图仅将您的数据转换为小部件,而gridview children参数需要小部件列表则返回单个小部件,因此您必须将其隐藏到列表中。
children: snapshot.data
.map((photo)=>buildItemPhoto(photo)).toList(), //convert to list
);