Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:io';
- import 'package:huntstreet/model/wishlist/wishlist_model.dart';
- import 'package:path/path.dart';
- import 'package:path_provider/path_provider.dart';
- import 'package:sqflite/sqflite.dart';
- class DBProvider {
- DBProvider._();
- static Database _database;
- static final DBProvider db = DBProvider._();
- static final String _table = "Wishlist";
- Future<Database> get database async {
- if (_database != null) return _database;
- _database = await initDatabase();
- return _database;
- }
- Future<Database> initDatabase() async {
- Directory documentsDirectory = await getApplicationDocumentsDirectory();
- String dbPath = join(documentsDirectory.path, "wishlist.db");
- return await openDatabase(
- dbPath,
- version: 1,
- onCreate: (Database db, int version) async {
- await db.execute(
- "CREATE TABLE $_table"
- "("
- "product_id INT PRIMARY KEY"
- ")",
- );
- },
- );
- }
- Future<void> newWishlist(Wishlist wishlist) async {
- final Database db = await database;
- List<Map<String, dynamic>> res = await db.query(_table);
- if (res.isEmpty) await db.insert(_table, wishlist.toMap());
- }
- Future<List<Wishlist>> getAllWishlist() async {
- final Database db = await database;
- List<Map<String, dynamic>> res = await db.query(_table);
- List<Wishlist> listCart =
- res?.map((c) => Wishlist.fromMap(c))?.toList() ?? [];
- return listCart.length == 0 ? null : listCart;
- }
- Future<void> deleteWishlist(int id) async {
- final Database db = await database;
- db.delete(
- _table,
- where: "product_id = ?",
- whereArgs: [id],
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement