我如何在flutter的sqlite数据库中保存嵌套的复杂json数据

时间:2019-07-28 21:25:55

标签: sql json sqlite flutter dart

我有嵌套复杂的Json 数据。 我为他们制作了模型,我可以serlizie和de serlize。

我阅读了颤动的文档以及将数据保存在sqlite中的示例 但是所有示例都是针对简单的json数据,而不是复杂的数据

我为此创建了两个表(oder_table,order_item_table),并将order_id密钥保存在order_item_table中

但是给我嵌套地图的查询有点奇怪。

我该怎么办?

class Order {
  int id;
  int userId;
  OrderStatus status;
  List<OrderItem> orderItems;
  DateTime createdAt;
  DateTime updatedAt;

  Order({
    this.id,
    this.userId,
    this.status,
    this.orderItems,
    this.createdAt,
    this.updatedAt,
  });

  factory Order.fromJson(Map<String, dynamic> json) => new Order(
        id: json["id"],
        userId: json["user_id"],
        status: OrderStatusValues.map[json["status"]],
        orderItems: new List<OrderItem>.from(
            json["order_items"].map((x) => OrderItem.fromJson(x))),
        createdAt: DateTime.parse(json["created_at"]),
        updatedAt: DateTime.parse(json["updated_at"]),
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "user_id": userId,
        "status": OrderStatusValues.reverse[status],
        "order_items":
            new List<dynamic>.from(orderItems.map((x) => x.toJson())),
        "created_at": createdAt.toIso8601String(),
        "updated_at": updatedAt.toIso8601String(),
      };
}

class OrderItem {
  int id;
  int orderId;
  int productId;
  int qty;
  Product product;
  OrderItemStatus status;
  String comment;
  DateTime createdAt;
  DateTime updatedAt;

  OrderItem({
    this.id,
    this.orderId,
    this.productId,
    this.qty,
    this.product,
    this.status,
    this.comment,
    this.createdAt,
    this.updatedAt,
  });

  factory OrderItem.fromJson(Map<String, dynamic> json) => new OrderItem(
        id: json["id"],
        orderId: json["order_id"],
        productId: json["product_id"],
        qty: json["qty"],
        product: Product.fromJson(json['product']),
        status: orderItemStatusValues.map[json["status"]],
        comment: json["comment"] == null ? null : json["comment"],
        createdAt: DateTime.parse(json["created_at"]),
        updatedAt: DateTime.parse(json["updated_at"]),
      );



  Map<String, dynamic> toJson() => {
        "id": id,
        "order_id": orderId,
        "product_id": productId,
        "qty": qty,
        'product': product.toJson(),
        "status": orderItemStatusValues.reverse[status],
        "comment": comment == null ? null : comment,
        "created_at": createdAt.toIso8601String(),
        "updated_at": updatedAt.toIso8601String(),
      };
}

我创建了这样的表:

Future<Database> getDatabase() async {
    var databasesPath = await getDatabasesPath();
    String path = join(databasesPath, 'stock.db');

    String sql = '''
    CREATE TABLE $_tableName (
      order_id INTEGER PRYMARY KEY,
      user_id INTEGER,
      order_status TEXT,
      created_at TEXT,
      updated_at TEXT
    )
    ''';

    String sql2 = '''
      CREATE TABLE $_order_item_table (
        order_item_id INTEGER PRYMARY KEY,
        order_id INTEGER,
        product_id INTEGER,
        qty INTEGER,
        order_item_status TEXT,
        comment TEXT,
        order_item_created_at TEXT,
        order_item_updated_at TEXT
      )
    ''';
    database = await openDatabase(path, version: 1,
        onCreate: (Database db, int version) async {
      await db.execute(sql);
      await db.execute(sql2);
    });

    return database;
  }

0 个答案:

没有答案