0

这是我存储在数据库中的数据

[{count:3,minced:0,category_id:3,size_id:63,chops_id:null,cookie_id:null,choole:null,notes:},{count:4,minced:0,category_id:3,size_id:62 ,chops_id:null,cookie_id:null,choole:null,notes:},{count:2,minced:2,category_id:2,size_id:49,chops_id:8,cookie_id:8,choole:2,notes:bzbznzjz} ]

以及我需要发送给 API 的内容

[{"size_id":59,"count":2,"category_id":2,"chops_id":null,"cookie_id":null,"choole":null,"notes":"jgg","minced": 0},{"size_id":63,"count":3,"category_id" :2,"chops_id":4,"cookie_id":8,"choole":2,"notes":"tvv","minced “:1}]

我需要将此“”添加到我的密钥中,我该怎么做?这就是我的代码

import 'dart:async';
import 'dart:io';

import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';

import 'Models/product.dart';

class DatabaseHelper {
  Product _product;
  static final DatabaseHelper _instance = new DatabaseHelper.internal();

  factory DatabaseHelper() => _instance;

  static Database _db;

  Future<Database> get db async {
    if (_db != null) return _db;
    _db = await initDb();
    return _db;
  }

  DatabaseHelper.internal();

  initDb() async {
   Directory documentsDirectory = await getApplicationDocumentsDirectory();
    String path = join(documentsDirectory.path, "main.db");
    var theDb = await openDatabase(path, version: 1, onCreate: _onCreate);
    return theDb;
  }

  void _onCreate(Database db, int version) async {
    await db.execute(
        "CREATE TABLE Product(id INTEGER PRIMARY KEY, count INTEGER, minced INTEGER, rate DOUBLE, category_id INTEGER, size_id INTEGER, chops_id INTEGER, cookie_id INTEGER, choole INTEGER, notes TEXT, name TEXT, image TEXT, size_name TEXT, category_name TEXT)");  }

  Future<int> saveProduct(Product product) async {
    var dbProduct = await db;
    int res = await dbProduct.insert("Product", product.toMap());
    print(product.toMap());
    return res;
  }

    Future<List> getAllProduct() async {
      var dbProduct = await db;
      var result = await dbProduct.rawQuery("SELECT * FROM Product");
      return result.toList();
    }
  Future<List> getCartProduct() async {
    var dbProduct = await db;
    var result = await dbProduct.rawQuery("SELECT count, minced, category_id, size_id, chops_id, cookie_id, choole, notes FROM Product");
    return result;
  }


  Future<int> getCount() async {
      var dbProduct = await db;
      return Sqflite.firstIntValue(
          await dbProduct.rawQuery("SELECT COUNT(*) FROM Product"));
    }


    Future<Product> getProduct(int id) async {
      var dbProduct = await db;

      var result = await dbProduct.rawQuery("SELECT * FROM Product WHERE id = $id");
      if (result.length == 0) return null;
      return new Product.fromMap(result.first);
    }


  Future<int> deleteProducts(Product product) async {
    var dbProduct = await db;

    int res = await dbProduct
        .rawDelete('DELETE FROM Product WHERE id = ?', [product.id]);
    return res;
  }

   update(Product product) async {
    var dbProduct = await db;

    int res = await dbProduct.update("Product", product.toMap(),
        where: "id = ?", whereArgs: <int>[product.id]);

    return res > 0 ? true : false;
  }
}

4

4 回答 4

1

兄弟,据我所知,存储在 DB 中的数据不在 Json 字符串中,所以首先你需要做的是

  1. 以正确的格式保存数据使用模型类通过使用“jsonEncode(您的产品列表)”将数据保存为字符串

将用户视为模型类

  class User {
  final String name;
  final String email;

  User(this.name, this.email);

  User.fromJson(Map<String, dynamic> json)
      : name = json['name'],
        email = json['email'];

  Map<String, dynamic> toJson() =>
    {
      'name': name,
      'email': email,
    };
}

每当您将数据保存到数据库时,您都应该使用

String json = jsonEncode(user);

String json = jsonEncode(List<user>);

这样,您将在数据库中获得 "。

将数据检索回 json 只需在此处传递存储的字符串

List<Users> MyList = (StringFromDB as List)
      .map((data) => User.fromJson(data))
      .toList();

这将解决您的问题

于 2019-08-20T10:37:58.900 回答
0

只需toMap在您的数据类中添加一个方法。例如:

class DataClass {
  int id;
  String message;

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'message': message
    }
  }

}

于 2019-08-19T15:44:47.730 回答
0

据我所知,您只需要从您的对象中制作 json 即可。在您的 Product 类中添加 toJson 然后调用它。

 int res = await dbProduct.insert("Product", product.toMap());

所以像这样:

 int res = await dbProduct.insert("Product", product.toJson());

这是序列化文档在这种情况下总是有帮助的。

于 2019-08-20T10:46:06.983 回答
0

通过 API,我猜你的意思是 Web API。如果它期待 json (如果这就是你所说的“”),你应该jsonEncode你的数据(列表或地图)

于 2019-08-20T10:17:54.870 回答