Flutter Cloud Firestore 出现转换器错误

问题描述 投票:0回答:1

我按照文档here在读取和写入云Firestore时使用自定义对象。

客户端类:

class Client {
  String id;
  String name;
  String email;
  int contactNumber;

  Client({
    required this.id,
    required this.name,
    required this.email,
    required this.contactNumber,
  });

  factory Client.fromFirestore(
    DocumentSnapshot<Map<String, dynamic>> snapshot,
    SnapshotOptions? options,
  ) {
    final data = snapshot.data();
    return Client(
      id: data?['id'],
      name: data?['name'],
      email: data?['email'],
      contactNumber: data?['contactNumber'],
    );
  }

  Map<String, dynamic> toFirestore() {
    return {
      "id": id,
      "name": name,
      "email": email,
      "contactNumber": contactNumber
    };
  }
}

与 withConverter 一起创建集合引用:

  CollectionReference usersCollection = FirebaseFirestore.instance
      .collection('users')
      .withConverter(
          fromFirestore: Client.fromFirestore,
          toFirestore: (Client client, options) => client.toFirestore());

但是我收到以下错误:

参数类型“Map Function(Client, SetOptions?)”无法分配给参数类型“Map Function(Object?, SetOptions?)”。

不确定我在这里缺少什么。

flutter firebase google-cloud-firestore
1个回答
0
投票

尝试更新数据类型:

CollectionReference usersCollection = FirebaseFirestore.instance
      .collection('users')
      .withConverter(
          fromFirestore: Client.fromFirestore,
          toFirestore: (Client client, SetOptions? options) => client.toFirestore());  // update this line

© www.soinside.com 2019 - 2024. All rights reserved.