Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

flutter - Map of object containing a List<Object> for sqlite

I am setting up my model classes to confirm to the docs for sqflite which suggest including a named constructor to convert to/from Maps to better handling of data between the classes and the DB. Every example I can find is very simple, with class properties all being simple data types.

Using the constructor and method shown below, converting to/from Map is quite simple when dealing with a class such as this.

class Human{
  final String name;
  final String height;
  Final String weight;

  Human({this.name, this.height, this.weight});
}

However, when you have a class where one of the fields is a bit more complex, I do not understand how to structure things within the named constructor and xxx method to return the map of data that I 'believe' I should get.

class Human{
      final String name;
      final String height;
      Final String weight;
      List<Child> children = [];

      Human({this.name, this.height, this.weight, this.children});
    }

Human({this.name, this.height, this.weight, this.children});

  Human.fromMap(Map<String, dynamic> map)
    : name = map['name'],
      height = map['height'],
      weight = map['weight'],
      children = map['children'];

  Map<String, dynamic> toMap() {
   return {
     'name': name,
     'height': height,
     'weight': weight,
     'children': children,
   }; 
  }

The List children is the part I am struggling with. I believe you have to get each Child object ALSO converted to a map within the parent map, but am losing the battle here.

Is my approach way off here? Is there some other method I should be using to accomplish this?

Any assistance would be much appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here I am explaining the following

  1. How to convert a model object into Map to use with sqlite
  2. How to convert a Map object from sqlite into a model class.
  3. How to parse JSON reponse properly in flutter
  4. How to convert a model object into JSON

All of the above questions has same answer. Dart has great support for these operations. Here I am going to illustrate it with a detailed example.

class DoctorList{
  final List<Doctor> doctorList;

  DoctorList({this.doctorList});

  factory DoctorList.fromMap(Map<String, dynamic> json) {
    return DoctorList(
      doctorList: json['doctorList'] != null
          ? (json['doctorList'] as List).map((i) => Doctor.fromJson(i)).toList()
          : null,
    );
  }

  Map<String, dynamic> toMap() {
    final Map<String, dynamic> data = Map<String, dynamic>();
    if (this.doctorList != null) {
      data['doctorList'] = this.doctorList.map((v) => v.toMap()).toList();
    }
    return data;
  }
}

The above DoctorList class has a member which holds a list of 'Doctor' objects..

And see how I parsed the doctorList.

 doctorList: json['doctorList'] != null
      ? (json['doctorList'] as List).map((i) => Doctor.fromMap(i)).toList()
      : null,

You may wonder, how the Doctor class may look like. Here you go

class Doctor {
  final String doCode;
  final String doctorName;

  Doctor({this.doCode, this.doctorName});

  factory Doctor.fromMap(Map<String, dynamic> json) {
    return Doctor(
      doCode: json['doCode'],
      doctorName: json['doctorName'],
    );
  }

  Map<String, dynamic> toMap() {
    final Map<String, dynamic> data = Map<String, dynamic>();
    data['doCode'] = this.doCode;
    data['doctorName'] = this.doctorName;
    return data;
  }

}

That's all. Hope you got the idea. Cheers!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...