有没有办法在 Flutter 上为 firestore 数据库生成模型类?

Blo*_*oss 5 dart firebase flutter google-cloud-firestore

我目前正在手动执行此过程。这是一个示例数据类。有没有办法自动生成?

class RideModel {
  final String docId;
  final bool hasRequest;
  final String rideCollectionId;
  final String vehicleTypeId;
  final double corporationRate;
  final double driverRate;
  final double driverInsurance;
  final double passengerInsurance;
  final List startUpCharge;
  final double waitingCharge;
  final double normalCharge;

  RideModel({this.docId, this.hasRequest, this.rideCollectionId,this.vehicleTypeId, this.corporationRate, this.driverRate, this.driverInsurance, this.passengerInsurance, this.startUpCharge, this.waitingCharge, this.normalCharge});

  factory RideModel.fromFirestore(DocumentSnapshot doc) {
    var data = doc.data;

    return RideModel(
        docId: data['docId'],
        hasRequest: data['hasRequest'],
        rideCollectionId: data['rideCollectionId'],
        vehicleTypeId: data['tripDetails']['vehicleType']['vehicleTypeId'],
        corporationRate: data['tripDetails']['vehicleType']['corporation'].toDouble()??0.0,
        driverRate: data['tripDetails']['vehicleType']['driver'].toDouble()??0.0,
        driverInsurance: data['tripDetails']['vehicleType']['driverInsurance'].toDouble()??0.0,
        passengerInsurance: data['tripDetails']['vehicleType']['passengerInsurance'].toDouble()??0.0,
        normalCharge: data['tripDetails']['vehicleType']['normalCharge'].toDouble()??0.0,
        startUpCharge: data['tripDetails']['vehicleType']['startUpCharge']??[],
        waitingCharge: data['tripDetails']['vehicleType']['waitingCharge'].toDouble()??0.0);
  }
}
Run Code Online (Sandbox Code Playgroud)

Ral*_*mos 3

正如 @Alex Sunder Singh 在评论和此社区帖子的答案之一中提到的,您可以使用JsonSerialized()来执行此操作。

为了使用它,您必须在 pubspec.yaml 上设置这些依赖项

dependencies:
  # Your other regular dependencies here
  json_annotation: <latest_version>

dev_dependencies:
  # Your other dev_dependencies here
  build_runner: <latest_version>
  json_serializable: <latest_version>
Run Code Online (Sandbox Code Playgroud)

然后,将@JsonSerializable()注释添加到您的类并将包导入到您的类中,它看起来像这样

import 'package:json_annotation/json_annotation.dart';

@JsonSerializable()
class RideModel {
  final String docId;
  final bool hasRequest;
  final String rideCollectionId;
  final String vehicleTypeId;
  final double corporationRate;
  final double driverRate;
  final double driverInsurance;
  final double passengerInsurance;
  final List startUpCharge;
  final double waitingCharge;
  final double normalCharge;

  RideModel(this.docId, this.hasRequest, this.rideCollectionId,this.vehicleTypeId, this.corporationRate, this.driverRate, this.driverInsurance, this.passengerInsurance, this.startUpCharge, this.waitingCharge, this.normalCharge);

  factory RideModel.fromJson(Map<String, dynamic> json) => _$RideModelFromJson(json);

  Map<String, dynamic> toJson() => _$RideModelToJson(this);
}
Run Code Online (Sandbox Code Playgroud)

注意:@JsonSerializable(explicitToJson: true)如果您希望所有非原始对象都被描述为 json 而不是“对象实例”,请使用

最后,通过从终端运行代码生成实用程序来生成 JSON 序列化代码

flutter packages pub run build_runner build
Run Code Online (Sandbox Code Playgroud)

这样,您将能够toJson()将数据映射到您的对象并将fromJson()对象作为数据发送。

希望这可以帮助。