firebase 参数类型'StreamTransformer〈dynamic,dynamic>'不能赋给参数类型StreamTransformer〈QuerySnapshot List< Message>>

sqyvllje  于 2022-12-30  发布在  其他
关注(0)|答案(3)|浏览(111)

我正在尝试使用Flutter & Firebase构建一个聊天应用程序,我从Github获取了下面的代码,并将其用于我的项目。在Flutter的早期版本中,它可以正常工作,但在2.0版本中,它显示了以下错误:第一个月
错误由以下行触发:
.transform(Utils.transformer(User.fromJson));
完整代码:

class FirebaseApi {
  static Stream<List<User>> getUsers() => FirebaseFirestore.instance
      .collection('users')
      // .collection('ch ats')
      .orderBy(UserField.timestamp, descending: true)
      .snapshots()
      .transform(Utils.transformer(User.fromJson));

  static Future uploadMessage(String idUser, String message) async {
    final refMessages =
        FirebaseFirestore.instance.collection('chats/$idUser/messages');

    final newMessage = Message(
      // idUser: myId,
      idUser: Globals.auth.currentUser!.uid,
      // urlAvatar: myUrlAvatar,
      urlAvatar: ChatPageState.photoUrl.toString(),
      // username: myUsername,
      username: ChatPageState.displayName.toString(),
      message: message,
      createdAt: DateTime.now(),
    );
    await refMessages.add(newMessage.toJson());

    final refUsers = FirebaseFirestore.instance.collection('users');

    await refUsers.doc(idUser).update({UserField.timestamp: DateTime.now()});
  }

  static Stream<List<Message>> getMessages(String idUser) {
    return FirebaseFirestore.instance
        .collection('chats/$idUser/messages')
        .orderBy(MessageField.createdAt, descending: true)
        .snapshots()
        .transform(Utils.transformer(Message.fromJson));
  }

}

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

          sink.add(users);
        },
      );

  static DateTime toDateTime(Timestamp value) {
    // if (value == null);

    return value.toDate();
  }

  static dynamic fromDateTimeToJson(DateTime date) {
    if (date == null) return null;

    return date.toUtc();
  }
}
kognpnkq

kognpnkq1#

添加QuerySnapshot<Map<String, dynamic>>作为你的返回类型。对我来说很有效。

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<Map<String, dynamic>> data, EventSink<List<T>> sink) {
      final snaps = data.docs.map((doc) => doc.data()).toList();
      final users = snaps.map((json) => fromJson(json)).toList();

      sink.add(users);
    },
  );
ffdz8vbo

ffdz8vbo2#

这里的问题是transformer函数的返回类型是dynamic,而. transform函数需要特定的返回类型,解决方法是增加返回类型,如下所示:

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

      sink.add(users);
    },
  );
5vf7fwbs

5vf7fwbs3#

因此,对于遇到此问题任何人,请将您的依赖项版本更改为
Firebase _核心:^0.5.0 + 1
云_消防商店:^0.14.1 + 3
它对我很有效,它的依赖性问题

相关问题