Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Model with nested Map to set on Firebase (json_annotation.dart)

My Firebase Database model looks like:

dbRef
    |___userId
             |____Cars
                  |____Alfa_Romeo
                  |             |____Year: 1992
                  |             |____Price: 10000
                  |__________Audi
                  |             |____Year: 1998
                  |             |____Price: 3000
                  |___________BMW
                                |____Year: 2001
                                |____Price: 7000

Then I have a Class Car that looks like

import 'package:json_annotation/json_annotation.dart';


part 'car.g.dart';

/// An annotation for the code generator to know that this class needs the
/// JSON serialization logic to be generated.
@JsonSerializable()

class Car {
  Carta(this.model, this.details);

  String model;
  List details;

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

  Map<String, dynamic> toJson() => _$CarToJson(this);
}

Now when I set the data to Firebase I get this:

dbRef
    |___userId
              |____Cars
                      |____Alfa_Romeo
                      |____Year: 1992
                      |____Price: 10000

... and I want it to be like the model from the first diagram. How can I nest the details in the "Model" child? Can anyone help me with this, please?

EDIT: Just to be sure I'm clear what I want. This code is from the Flutter Team docs example:

import 'address.dart';
import 'package:json_annotation/json_annotation.dart';
part 'user.g.dart';

@JsonSerializable(explicitToJson: true)
class User {
  String firstName;
  Address address;

  User(this.firstName, this.address);

  factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
  Map<String, dynamic> toJson() => _$UserToJson(this);
}

This code generates the following output:

name: John, address: {street: My st., city: New York}}

And what I want to achieve is John to became a Child key and the Adress to be nested inside, because there could be more than one Adress Array. The Adress field should become a Key (it will be unique) and then it will have a map of items and each item have only 2 fields description and price.

like image 418
John Smith Optional Avatar asked Jan 18 '20 16:01

John Smith Optional


1 Answers

I suggest breaking the details out into their own class like this example, https://flutter.dev/docs/development/data-and-backend/json#generating-code-for-nested-classes.

Edit

Rather than using JsonSerializable, try mapping the model manually and copying the remaining properties to another Map.

class Car {
  Car({this.model, this.details});

  String model;
  Map<String, dynamic> details;

  factory Car.fromJson(Map<String, dynamic> json) {
    Map<String, dynamic> details = Map<String, dynamic>.from(json);
    details.remove('model');

    return new Car(
      model: json['model'],
      details: details
    );
  }

  Map<String, dynamic> toJson() {
    Map<String, dynamic> json = Map<String, dynamic>.from(details);
    json['model'] = model;

    return json;
  }
}
like image 76
Jacob McGowan Avatar answered Oct 26 '22 23:10

Jacob McGowan