作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在手动执行此过程。这是一个示例数据类。有没有办法自动生成?
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 而不是“对象实例”
flutter packages pub run build_runner build
toJson()
将数据映射到您的对象和
fromJson()
将您的对象作为数据发送。
关于firebase - 有没有办法在 Flutter 上为 firestore 数据库生成模型类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60523430/
我是一名优秀的程序员,十分优秀!