简体   繁体   English

从 StreamBuilder BloC firestore Flutter 获取数据时出错

[英]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:我在 Flutter 中的 APP 出现问题...我试图从 Bloc 和 Repositore 和 Firestore 获取数据,它显示在我的 GridView 中,但出现此错误:

在此处输入图像描述

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:我的 Bloc 代码,它有 Stream 到我的回购中的 PhotoDish 列表:

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:我的回购代码调用从 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);
      });
  }

I think you are just miss to add.ToList() method at the end where you are mapping list into widget.我认为您只是想在将列表映射到小部件的末尾添加.ToList() 方法。

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. Map 仅将您的数据转换为小部件,它返回单个小部件,而 gridview 子参数需要小部件列表,因此您必须将其转换为列表。

children: snapshot.data
                    .map((photo)=>buildItemPhoto(photo)).toList(), //convert to list
              );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM