我需要将一些存储在数据库中的数据发送到 API?
I need to send some data stored in database to API?
这是我存储在数据库中的数据
[{count: 3, minced: 0, category_id: 3, size_id: 63, chops_id: null, cookie_id: null, choole: null , 笔记: }, {count: 4, minced: 0, category_id: 3, size_id: 62, chops_id: null, cookie_id: null, choole: null,笔记:},{计数:2,切碎:2,category_id:2,size_id:49,chops_id:8,cookie_id:8,choole:2,笔记: 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;
}
}
只需在您的数据 类 中添加一个 toMap
方法。例如:
class DataClass {
int id;
String message;
Map<String, dynamic> toMap() {
return {
'id': id,
'message': message
}
}
}
API,我猜你指的是 Web API。如果它期望 json(如果这就是你所说的“”),你应该 jsonEncode
你的数据(列表或地图)
兄弟,据我所知,数据库中存储的数据不在 Json 字符串中,所以首先您需要做的是
- 以正确的格式保存数据使用模型 class 通过使用 "jsonEncode(Your Products List)"
将数据保存为字符串
将用户视为模型 class
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();
这将解决您的问题
据我所知,您只需要从您的对象中制作出 json。在您的产品中添加 toJson class 然后调用它。
int res = await dbProduct.insert("Product", product.toMap());
像这样:
int res = await dbProduct.insert("Product", product.toJson());
这是序列化 doc 在这种情况下总是有用的。
这是我存储在数据库中的数据
[{count: 3, minced: 0, category_id: 3, size_id: 63, chops_id: null, cookie_id: null, choole: null , 笔记: }, {count: 4, minced: 0, category_id: 3, size_id: 62, chops_id: null, cookie_id: null, choole: null,笔记:},{计数:2,切碎:2,category_id:2,size_id:49,chops_id:8,cookie_id:8,choole:2,笔记: 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;
}
}
只需在您的数据 类 中添加一个 toMap
方法。例如:
class DataClass {
int id;
String message;
Map<String, dynamic> toMap() {
return {
'id': id,
'message': message
}
}
}
API,我猜你指的是 Web API。如果它期望 json(如果这就是你所说的“”),你应该 jsonEncode
你的数据(列表或地图)
兄弟,据我所知,数据库中存储的数据不在 Json 字符串中,所以首先您需要做的是
- 以正确的格式保存数据使用模型 class 通过使用 "jsonEncode(Your Products List)" 将数据保存为字符串
将用户视为模型 class
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();
这将解决您的问题
据我所知,您只需要从您的对象中制作出 json。在您的产品中添加 toJson class 然后调用它。
int res = await dbProduct.insert("Product", product.toMap());
像这样:
int res = await dbProduct.insert("Product", product.toJson());
这是序列化 doc 在这种情况下总是有用的。