0

That's my data stored in database

[{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}]

and that what I need to send to 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}]

I need to add this "" to my keys how can I do this? and that's my code

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 Answers 4

1

Brother as per my Knowledge the Data Stored in DB is not in Json String so First of all what you need to do is

  1. Save the Data in Correct format use Model class to Save data as String by using "jsonEncode(Your Products List)"

Consider User as Model 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,
    };
}

whenever you save data to DB you should use

String json = jsonEncode(user);

String json = jsonEncode(List<user>);

by this you will get the " in your DB.

to retrive the Data back in to json just pass the Stored String here

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

this will solve your Problem

Sign up to request clarification or add additional context in comments.

Comments

0

Just add a toMap method in your data classes. For example:

class DataClass {
  int id;
  String message;

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

}

2 Comments

that is my product model and didn't work class Product { int id; int count; Product( this.id, this.count,); Product.map(dynamic obj) { this.id = obj["id"]; this.count = obj["count"];} int get _id => id; int get _count => count; Map<String, dynamic> toMap() { var map = new Map<String, dynamic>(); map["count"] = _count; if (_id != null) { map["id"] = id;} return map; } void setProductId(int id) {this.id = id;} Product.fromMap(Map<String, dynamic> map) {this.id = map["id"]; this.count = map["count"]; }}
What kind of error did you get? If you want the data to match the expected result in your initial post, you will have to wrap the Map with an array, and if you want it to work for sending to an API you will have to jsonEncode the whole thing.
0

By API, I guess you mean a Web API. If it is expecting json (if that is what you mean by ""), you should jsonEncode your data (list or map)

Comments

0

From what I see you just need to make json out of your object. Add toJson in your Product class then just call it.

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

So like this:

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

This is the serialization doc is always helpful in such a case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.