Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. import 'dart:io';
  2. import 'package:huntstreet/model/wishlist/wishlist_model.dart';
  3. import 'package:path/path.dart';
  4. import 'package:path_provider/path_provider.dart';
  5. import 'package:sqflite/sqflite.dart';
  6.  
  7. class DBProvider {
  8. DBProvider._();
  9.  
  10. static Database _database;
  11. static final DBProvider db = DBProvider._();
  12. static final String _table = "Wishlist";
  13.  
  14. Future<Database> get database async {
  15. if (_database != null) return _database;
  16. _database = await initDatabase();
  17. return _database;
  18. }
  19.  
  20. Future<Database> initDatabase() async {
  21. Directory documentsDirectory = await getApplicationDocumentsDirectory();
  22. String dbPath = join(documentsDirectory.path, "wishlist.db");
  23. return await openDatabase(
  24. dbPath,
  25. version: 1,
  26. onCreate: (Database db, int version) async {
  27. await db.execute(
  28. "CREATE TABLE $_table"
  29. "("
  30. "product_id INT PRIMARY KEY"
  31. ")",
  32. );
  33. },
  34. );
  35. }
  36.  
  37. Future<void> newWishlist(Wishlist wishlist) async {
  38. final Database db = await database;
  39. List<Map<String, dynamic>> res = await db.query(_table);
  40. if (res.isEmpty) await db.insert(_table, wishlist.toMap());
  41. }
  42.  
  43. Future<List<Wishlist>> getAllWishlist() async {
  44. final Database db = await database;
  45. List<Map<String, dynamic>> res = await db.query(_table);
  46. List<Wishlist> listCart =
  47. res?.map((c) => Wishlist.fromMap(c))?.toList() ?? [];
  48. return listCart.length == 0 ? null : listCart;
  49. }
  50.  
  51. Future<void> deleteWishlist(int id) async {
  52. final Database db = await database;
  53. db.delete(
  54. _table,
  55. where: "product_id = ?",
  56. whereArgs: [id],
  57. );
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement