【问题标题】:Flutter Firestore StreamTransformer DocumentSnapshot error [duplicate]Flutter Firestore StreamTransformer DocumentSnapshot 错误 [重复]
【发布时间】:2021-01-10 09:58:53
【问题描述】:

我正在练习 Flutter 以使用 firestore 开发一个简单的实时聊天应用程序,之前我使用 cloud_firestore 0.13.7 并且工作正常。更新到新版 cloud_firestore 0.14.0+2 后,代码中有一个重大更改,我不知道如何修改它。

repository_service.dart

class RepositoryService {
final CollectionReference _usersCollectionReference =
      FirebaseFirestore.instance.collection('users');

Stream<DocumentSnapshot> getChatList(String id) {
    return _userChatsCollectionReference.doc(id).snapshots();
  }
}

chat_services.dart

class chat_services(){

RepositoryService _repositoryService = locator<RepositoryService>();

Stream<List<String>> getChatList(String id) {
    return _repositoryService.getChatList(id).transform(documentToChatListTransformer);
  }

  StreamTransformer documentToChatListTransformer = StreamTransformer<DocumentSnapshot, List<String>>.fromHandlers(
    handleData: (DocumentSnapshot snapShot, EventSink<List<String>> sink) {
      if (snapShot.exists) {
        sink.add(snapShot.data.keys.toList());
      } else {
        sink.add([]);
      }
    }
  );

}

错误是 keys 来自 chat_services.dart 中的函数 documentToChatListTransformer

没有为类型 'Map 定义 getter 'keys' 功能()'。尝试导入定义“键”的库,更正 现有 getter 名称的名称,或定义 getter 或 名为 'keys'.dart(undefined_getter) 的字段

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    这里,datamethod on DocumentSnapshot,而不是属性。您需要调用带括号的方法:

    snapShot.data().keys.toList()
    

    【讨论】:

      猜你喜欢
      • 2021-02-26
      • 2019-11-05
      • 2019-01-25
      • 1970-01-01
      • 2022-01-01
      • 2021-03-09
      • 2021-03-15
      • 2021-11-11
      • 1970-01-01
      相关资源
      最近更新 更多