1

I'm new at Flutter and I'm trying to handle the complex json structure but i dont know how to store complex JSON data into the firebase realtime database using flutter.Here I attached my JSON structure and Data model class structure too. Are there are any related article or tutorialregarding the same.

JSON Structure:
{
  "users": {
    "[email protected]": {
      "name": "John Doe",
            "Country": "United Kingdom",
        "verified_phone": true,
        "ID_verification": False,
    },
      "Properties": {
        "Properties":[ {
        "propertiename":"Hilton_Common",
                        "Role" : "owner"},
    {"propertiename":"Carraige_guest_house",
                        "Role" : "staff",
            }
    ....
    ....
    ]},
      "Reservations": {
    "Properties": [{
         "propertiename":"Hilton_Common"
                        "Guest" : "Primary_guest"},
    {"propertiename":"Carraige_guest_house"
                        "Guest" : "Aditional",
        }
    ....
    ....
    ]},
          }}

User model:

class User {
  String key;
  final List<Host> host;
  final List<User_Reservation> reservation;
  String nameformlogin, phone, email;
  String verifiedphone, idverification;
  var list;
  User({
    this.key,
    this.nameformlogin,
    this.phone,
    this.email
    this.verifiedphone,
    this.idverification,
    this.host,
    this.reservation,

  });

  factory User.fromJson(Map<String, dynamic> json) {

    return new User(
        nameformlogin: json["nameformlogin"],
        phone: json["phone"],
        email: json["email"],
        verifiedphone: json["verifiedphone"],
        idverification: json["idverification"],
        host: json["host"],
        reservation:json["reservation"]
        );
  }

  toJson() {
    return {
        'name': nameformlogin,
        'county':phone,
        'email': email,
        'verifiedphone':verifiedphone,
        'idverification':idverification,
        'host':host,
    'reservation':reservation

    };
      }
}
class Host {
  String propertiesname, role;
  Host({this.propertiesname, this.role});
  factory Host.fromJson(Map<String, dynamic> json) {
    return new Host(propertiesname: json['propertiesname'], role: json['role']);
  }
}
class User_Reservation {
  String propertiesname, guest;
  User_Reservation({this.propertiesname, this.guest});
  factory User_Reservation.fromJson(Map<String, dynamic> json) {
    return new User_Reservation(
        propertiesname: json['propertiesname'], guest: json['guest']);
  }
}

1 Answer 1

2

create instance of Realtime Database (rtd) reference, Store data with as key value:

void storeData{
      dataBaseRef.child(user.key).set({
         "name":user.name,
         "host":user.host,
         ...
      });
}

If u don't know how to connect and create a reference of RTD, you can refer this youtube tutorial

Sign up to request clarification or add additional context in comments.

5 Comments

The variable name is not a problem. It is a simple string variable and I am able to store it in the database. The actual problem here for eg: host which is an array of a complex variable with structure------ class Host { String propertiesname, role; Host({this.propertiesname, this.role}); factory Host.fromJson(Map<String, dynamic> json) { return new Host(propertiesname: json['propertiesname'], role: json['role']); }............................................................Could you please provide a suggestion about how to store a complex variable(class within a class)?
@feathersoft when you use user.host (class within a class) to store the array of host object it will be stored successfully, have u tried once with the above code ?
@sumeetJain, I have tried but not able to save. But it didn't work. Do you have any sample code for saving data like <list> of class within another class?
@feathersoft i got the problem now, you need to run a loop over the list <class> create a key value pair of all the properties of the object inside list <class> and then add to the RTD with unique index. This might help you stackoverflow.com/questions/55034729/…
@sumeetJain, Do you have any flutter code to handle complex class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.