gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 02:46:40 25 4
gpt4 key购买 nike

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

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);
}
}

最佳答案

正如@Alex Sunder Singh 在评论中以及对此 Community post 的其中一个答案中提到的那样,您可以使用 JsonSerializable()去做吧。

为了使用它,您必须在 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>

然后,添加 @JsonSerializable()给你的类(class)注解以及将包导入你的类(class),它看起来像这样

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);
}

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

最后,通过从终端运行代码生成实用程序来生成 JSON 序列化代码
flutter packages pub run build_runner build

这样你就可以使用 toJson()将数据映射到您的对象和 fromJson()将您的对象作为数据发送。

希望这可以帮助。

关于firebase - 有没有办法在 Flutter 上为 firestore 数据库生成模型类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60523430/

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