gpt4 book ai didi

firebase - 如何在 Flutter 中使用 Firestore withConverter

转载 作者:行者123 更新时间:2023-12-04 12:13:55 31 4
gpt4 key购买 nike

我最近看到了cloud_firestore: ^2.0.0更新带来了新的 withConverter 功能。
我想用它来检索我的模型并将其传递到 Firestore,但我不太确定如何使用现有代码执行此操作。
例如,如何更新以下代码以使用我的自定义模型?

FirebaseFirestore.instance.collection('movies').add({
'length': 123,
'rating': 9.7,
});

最佳答案

Firestore 类型
首先,从 2.0.0 开始, 所有 Firestore 引用 & 查询 现在 输入 .这意味着 CollectionReference<T> , DocumentReference<T> , 和 Query<T>现在都有一个泛型类型参数 T .
默认
默认情况下(例如,当调用 FirebaseFirestore.instance.collection() 时),这个泛型是 Map<String, dynamic> .这意味着(不调用 withConverter ),你传递的数据和你接收的数据总是类型 Map<String, dynamic> .withConverter现在,您可以使用 withConverter在各个地方为了更改 这个类型:

  • DocumentReference.withConverter :

  • final modelRef = FirebaseFirestore.instance
    .collection('models')
    .doc('123')
    .withConverter<Model>(
    fromFirestore: (snapshot, _) => Model.fromJson(snapshot.data()!),
    toFirestore: (model, _) => model.toJson(),
    );
  • CollectionReference.withConverter :

  • final modelsRef =
    FirebaseFirestore.instance.collection('models').withConverter<Model>(
    fromFirestore: (snapshot, _) => Model.fromJson(snapshot.data()!),
    toFirestore: (model, _) => model.toJson(),
    );
  • Query.withConverter :

  • final personsRef = FirebaseFirestore.instance
    .collection('persons')
    .where('age', isGreaterThan: 0)
    .withConverter<Person>(
    fromFirestore: (snapshot, _) => Person.fromJson(snapshot.data()!),
    toFirestore: (model, _) => model.toJson(),
    );

    拨打 withConverter 时会发生什么是泛型类型 T设置为您的自定义 Model (例如,最后一个例子中的 Person)。这意味着对文档引用、集合引用或查询的每个后续调用都将使用该类型 而不是 Map<String, dynamic> .
    用法
    该方法的用法很简单:
  • 您通过了 FromFirestore 将快照(带选项)转换为自定义模型的函数。
  • 您通过了 ToFirestore 转换模型的函数 T (带选项)回到 Map<String, dynamic> ,即 Firestore 特定的 JSON 数据。

  • 例子
    这是使用 withConverter 的示例带定制 Movie模型类(请注意,我使用的是 freezed 因为它更具可读性):
    Future<void> main() async {
    // Create an instance of our model.
    const movie = Movie(length: 123, rating: 9.7);

    // Create an instance of a collection withConverter.
    final collection =
    FirebaseFirestore.instance.collection('movies').withConverter(
    fromFirestore: (snapshot, _) => Movie.fromJson(snapshot.data()!),
    toFirestore: (movie, _) => movie.toJson(),
    );

    // Directly add our model to the collection.
    collection.add(movie);
    // Also works for subsequent calls.
    collection.doc('123').set(movie);

    // And also works for reads.
    final Movie movie2 = (await collection.doc('2').get()).data()!;
    }

    @freezed
    class Movie with _$Movie {
    const factory Movie({
    required int length,
    required double rating,
    }) = _Movie;

    factory Movie.fromJson(Map<String, dynamic> json) => _$MovieFromJson(json);
    }

    关于firebase - 如何在 Flutter 中使用 Firestore withConverter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68079030/

    31 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com