I am trying to insert my data in sqllite
Here is my code
class Cart {
int id;
String title;
String image;
String price;
String color;
String sizeselect;
Cart({
this.id,
this.title,
this.image,
this.price,
this.color,
this.sizeselect,
});
factory Cart.fromJson(Map<String, dynamic> data) => new Cart(
id: data["id"],
title: data["title"],
image: data["image"],
price: data["price"],
color: data["color"],
sizeselect: data["sizeselect"],
);
Map<String, dynamic> toJson() => {
"id": id,
"title": title,
"image": image,
"price": price,
"color": color,
"sizeselect": sizeselect,
};
}
class DatabaseHelper {
static final _databaseName = "MyDatabase.db";
static final _databaseVersion = 1;
static final table = 'my_table';
// make this a singleton class
DatabaseHelper._privateConstructor();
static final DatabaseHelper instance = DatabaseHelper._privateConstructor();
// only have a single app-wide reference to the database
static Database _database;
Future<Database> get database async {
if (_database != null) return _database;
// lazily instantiate the db the first time it is accessed
_database = await _initDatabase();
return _database;
}
// this opens the database (and creates it if it doesn't exist)
_initDatabase() async {
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, _databaseName);
return await openDatabase(path,
version: _databaseVersion,
onCreate: _onCreate);
}
// SQL code to create the database table
Future _onCreate(Database db, int version) async {
await db.execute("CREATE TABLE $table ("
"id INTEGER PRIMARY KEY,"
"title TEXT,"
"image TEXT,"
"color TEXT,"
"price TEXT,"
"sizeselect TEXT"
")");
}
Future<int> insert(cart) async {
print(cart.id);
Database db = await instance.database;
return await db.insert(table, cart);
}
}
I am trying to pass like this
onPressed: () async {
var cart = Cart();
cart.id = widget.product.id;
cart.title = widget.product.title;
cart.image = widget.product.image;
cart.price = widget.product.normalPrice;
cart.color = selectedColor;
cart.sizeselect = selectedSize;
print(cart);
final dbHelper = DatabaseHelper.instance;
final id = await dbHelper.insert(cart);
// Model.createCustomer(map);
}
Its showing this error while passing data type 'Cart' is not a subtype of type 'Map<String, dynamic>'
Can any one please tell i need to convert it to something or what ? I think i need to change the json to String or something ? i just need to insert data in database but on this error i am stuck :/