【问题标题】:Flutter How to insert nested JSON array into different SQLite tablesFlutter 如何将嵌套的 JSON 数组插入到不同的 SQLite 表中
【发布时间】:2021-02-23 18:24:03
【问题描述】:

我希望我能清楚地解释我的需求,我有一个 API,我可以连接到它并从中接收 JSON。

Json 嵌套在一个列表和一个具有 2 层列表的对象中。

我想要的是将此 Josn 插入我的本地数据库(使用 SQLite 进行 Flutter)。

我到达的点是插入 json 数据的第一层,但无法插入在第二层中作为列表存在的数据。虽然我对其进行了序列化,但是当我尝试插入之后的列表,没有从列表表单到数据库表字段的映射。

就像我有 [Class User] 和 [Class Company] 一个用户有很多公司加入。

当从 JSON 中对数据进行序列化时,我让数据进入用户类,然后嵌套到公司类,并对 json 对象的第二层进行序列化,然后返回。

下一步是将数据插入数据库,但我在最后一个对象中的数据是这样的:

“用户名”:用户名 "company" : CompanyList --- 这是一个列表,不能以嵌套方式直接插入数据库。由于公司列表有多个字段。以及不止一项记录,例如:两家公司。

我相信会有一个映射反向执行此操作,因此我可以将其插入数据库(我在公司类中有它)但我不能从我尝试将数据插入数据库的地方调用它Classes 实例的区别

我手中的实例是一个User Type实例[两者之间的主类],而companyList如果要映射它需要一个Company实例类型。

我可以提供代码或解释更多,有关该方法的任何帮助或想法都可能会有所帮助。非常感谢!

#编辑 这是我尝试将数据插入数据库的地方

第一次插入没问题,但第二次不行。

  Future<int> createUserPermissions(UserPermissions userPermissions, ComListPermissions comListPermissions) async {
    final db = await dbProvider.database;
    var result = db.insert(userPermissionsDBTable, userPermissions.mapToDB());

    db.insert(userCompaniesTableDB, comListPermissions.toJson());
    return result;
  }

用户类

class UserPermissions {
  final String userName;
  final  List<ComListPermissions> comLists;
  final bool active;

  final String groupName;
  final int companyId;
  final String permissions;
  final ComListPermissions company;

  UserPermissions({this.company, this.groupName, this.companyId, this.permissions, this.userName, this.active, this.comLists});

  factory UserPermissions.mapFromJsonToDB(Map<String, dynamic> data) {
    if (data['comLists'] != null) {
      var companyObjJson = data['comLists'] as List;

      List<ComListPermissions> _companies = companyObjJson
          .map((companyJson) => ComListPermissions.fromJson(companyJson))
          .toList();

      return UserPermissions(
        userName: data['userName'],
        active: data['active'],
        comLists: _companies,
      );
    } else {
      return UserPermissions(
        userName: data['userName'],
        active: data['active'],
      );
    }
  }

  Map<String, dynamic> mapToDB() => {
        "userName": userName,
       // "comLists": comLists,
        "active": active,
      };

}

公司类

class ComListPermissions {
  ComListPermissions({
    this.groupName,
    this.companyId,
    this.permissions,
  });

  String groupName;
  int companyId;
  String permissions;
  //List<String> permissions;



  factory ComListPermissions.fromJson(Map<String, dynamic> json) {
    if (json['permissions'] != null) {
      var permissionsObjJson = json['permissions'] as List;
      String s = permissionsObjJson.toString();

      return ComListPermissions(
        groupName: json["groupName"],
        companyId: json["companyID"],
        permissions: s,
      );
    } else {
      return ComListPermissions(
        groupName: json["groupName"],
        companyId: json["companyID"],
      );
    }
  }

  Map<String, dynamic> toJson() => {
        "groupName": groupName,
        "companyID": companyId,
        "permissions": permissions,

    //"permissions": List<dynamic>.from(permissions.map((x) => x)),
      };
}

【问题讨论】:

  • 你找到什么了吗?
  • @TazeemSiddiqui 是的,只需添加一个 for 循环并在每次循环时发送索引,我在下面回答我的问题

标签: json sqlite flutter


【解决方案1】:

每次我需要在我的数据库中插入一个新的记录时,我都会添加一个 for 循环并发送索引

  Future<int> createUserPermissions(UserPermissions userPermissions) async {
    final db = await dbProvider.database;
    var result = db.insert(userPermissionsDBTable, userPermissions.mapToDB());
    int index = userPermissions.comLists.length;
    for (int i = 0; i < index; i++) {
      db.insert(userCompaniesTableDB, userPermissions.toDatabaseForCompany(i));
    }
    return result;
  }
  Map<String, dynamic> toDatabaseForCompany(int index) => {
    "companyID": comLists[index].companyId,
    "groupName": comLists[index].groupName,
    "permissions": comLists[index].permissions,
  };

【讨论】:

    猜你喜欢
    • 2021-08-16
    • 2020-03-04
    • 2016-04-18
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    • 2022-01-23
    • 2018-06-02
    • 2022-08-19
    相关资源
    最近更新 更多