is there a way to generate model class for firestore database on Flutter?

Viewed 5526

I am currently manually doing this process. This is a sample data class. Is there a way to generate automatically?

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

As mentioned by @Alex Sunder Singh on the comments and in one of the answers to this Community post, you can use JsonSerializable() to do it.

In order to use it, you must set these dependencies on you 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>

Then, add the @JsonSerializable() annotation to you class as well as importing the package to you class, it would look something like this

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

NOTE: use @JsonSerializable(explicitToJson: true) if you want all of your non-primitive objects to be described as json and not as 'Instance of Object'

Finally, generate the JSON serialization code by running the code generation utility from your terminal

flutter packages pub run build_runner build

With that you will be able to use toJson() to map data to you object and fromJson() to send your object as data.

Hope this helps.

If you use VS Code you can also just do ⌘ + . (Mac) and then click "Generate data class" this automatically generates a toJson and fromJson method based on the properties of the class.

If you have nested data these objects should also have a fromJson and toJson and so the "Generate data class" command can automatically use them in the fromJson and toJson method.

This way you don't need any external tool.

Related