简体   繁体   English

Flutter Stream Builder with Firestore 返回空列表

[英]Flutter Stream Builder with Firestore return empty list

Hey I tried to get data from firestore, the data is there, but the stream return empty list, any help will be appreciated!嘿,我试图从 firestore 获取数据,数据在那里,但 stream 返回空列表,任何帮助将不胜感激!

Here is my stream builder code:这是我的 stream 生成器代码:

StreamBuilder<List<ChatModel>>(
            stream: ChatRoom.getChats(id),
            builder: (context, snapshot) {
              switch (snapshot.connectionState) {
                case ConnectionState.waiting:
                  return Center(child: CircularProgressIndicator());
                default:
                  if (snapshot.hasError) {
                    print(snapshot.error);
                    return Center(
                        child: Text('Something Went Wrong Try later'));
                  } else {
                    if (snapshot.hasData) {
                      final message = snapshot.data;
                      print(message);
                      return ListView.separated(
                          itemBuilder: (context, index) => messageWidget(
                              message![index].message,
                              message![index].source == _userId),
                          separatorBuilder: (context, index) => SizedBox(
                              height: SizeConfig.safeBlockVertical * 3),
                          itemCount: snapshot.data!.length);
                    } else
                      return Center(child: Text('Say Hi..'));
                  }
              }
            }),

here is how I call the firestore:这是我如何称呼 firestore:

class ChatRoom {
  String id;
  ChatRoom({required this.id});

  static Stream<List<ChatModel>> getChats(String id) =>
    FirebaseFirestore.instance
      .collection(id)
      .orderBy(ChatField.sendAt, descending: false)
      .snapshots()
      .transform(Util.transformer(ChatModel.fromJson));
}

and here is how I transform the data:这是我转换数据的方式:

static StreamTransformer<QuerySnapshot<Map<String, dynamic>>, List<T>> transformer<T>(
  T Function(Map<String, dynamic> json) fromJson) =>
  StreamTransformer<QuerySnapshot<Map<String, dynamic>>, List<T>>.fromHandlers(
    handleData: (QuerySnapshot data, EventSink<List<T>> sink) {
      final snaps = data.docs.map((doc) => doc.data()).toList();
      final object = snaps.map((json) => fromJson(json as Map<String, dynamic>)).toList();

      sink.add(object);
    },
  );

here is the model这是 model

static ChatModel fromJson(Map<String, dynamic> json) => ChatModel(
  dest: json["dest"],
  destName: json['destName'],
  id: json['id'],
  message: json["message"],
  sendAt: json["sendAt"],
  source: json["source"],
  sourceName: json["sourceName"],
);

here is the screenshot of the firestore:这是 firestore 的屏幕截图: 在此处输入图像描述

Hello You have to check snapshot data if its null or not please see below code hope this works for you您好您必须检查快照数据是否为 null 请参阅下面的代码希望这对您有用

StreamBuilder<List<ChatModel>>(
            stream: ChatRoom.getChats(id),
            builder: (context, snapshot) {
              switch (snapshot.connectionState) {
                case ConnectionState.waiting:
                  return Center(child: CircularProgressIndicator());
                default:
                  if (snapshot.hasError) {
                    print(snapshot.error);
                    return Center(
                        child: Text('Something Went Wrong Try later'));
                  } else {
                    if(snapshot.data != null){
                    final message = snapshot.data;
                        return snapshot.data.size > 0 ?

                      print(message);
                     ListView.separated(
                          itemBuilder: (context, index) => messageWidget(
                              message![index].message,
                              message![index].source == _userId),
                          separatorBuilder: (context, index) => SizedBox(
                              height: SizeConfig.safeBlockVertical * 3),
                          itemCount: snapshot.data!.size) : 
                     Center(child: Text('Say Hi..'));
                    }
                    return Center(child: Text('Say Hi..'));

              }
            }),

Let me know if it's work.让我知道它是否有效。

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

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