包含List< Object>的对象的映射。对于sqlite [英] Map of object containing a List<Object> for sqlite

查看:80
本文介绍了包含List< Object>的对象的映射。对于sqlite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设置模型类,以确认sqflite的文档,该文档建议包含一个命名构造函数以与Maps进行相互转换,从而更好地处理类与DB之间的数据。我可以找到的每个示例都非常简单,类属性都是简单的数据类型。

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.

使用下面显示的构造函数和方法,当转换为Map或从Map转换时非常简单

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

但是,当您上课时,其中一个字段要多一些复杂,我不了解如何在命名构造函数和xxx方法中构造事物以返回我相信的数据映射。

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

列出孩子是我最努力的部分。我相信您还必须将每个Child对象也转换为父地图中的地图,但是在这里输掉了这场战斗。

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?

任何帮助将不胜感激。

推荐答案

在这里我要解释以下内容

Here I am explaining the following


  1. 如何将模型对象转换为Map以与sqlite结合使用

  2. 如何将Map对象从sqlite转换为模型类。

  3. 如何在抖动中正确解析JSON响应

  4. 如何将模型对象转换为JSON

  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

以上所有问题都具有相同的答案。 Dart对这些操作提供了大力支持。在这里,我将通过一个详细的示例进行说明。

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

DoctorList以上类具有一个成员,该成员拥有 医生对象的列表。

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

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

您可能想知道 Doctor 类的样子。

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!

这篇关于包含List&lt; Object&gt;的对象的映射。对于sqlite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆