Last active
September 15, 2020 11:53
-
-
Save nekonynn/f34111da059d418a656c00330e6ed23c to your computer and use it in GitHub Desktop.
SQFlite Implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:food_cookbook/repo/food_repo_contract.dart'; | |
| import 'package:path/path.dart'; | |
| import 'package:sqflite/sqflite.dart'; | |
| class DatabaseHelper { | |
| static final DatabaseHelper _instance = DatabaseHelper.internal(); | |
| factory DatabaseHelper() => _instance; | |
| static Database _db; | |
| DatabaseHelper.internal(); | |
| Future<Database> get db async { | |
| if (_db != null) { | |
| return _db; | |
| } | |
| _db = await initDb(); | |
| return _db; | |
| } | |
| initDb() async { | |
| String databasesPath = await getDatabasesPath(); | |
| String path = join(databasesPath, 'foodList.db'); | |
| var db = await openDatabase(path, version: 1, onCreate: _onCreate); | |
| return db; | |
| } | |
| void _onCreate(Database db, int newVersion) async { | |
| await db.execute( | |
| 'CREATE TABLE ${FoodRepoContract.tableName}' | |
| + '(' | |
| + '${FoodRepoContract.id} INTEGER PRIMARY KEY, ' | |
| + '${FoodRepoContract.idMeal} INTEGER, ' | |
| + '${FoodRepoContract.strMeal} TEXT, ' | |
| + '${FoodRepoContract.strMealThumb} TEXT, ' | |
| + '${FoodRepoContract.strCategory} TEXT' | |
| + ')' | |
| ); | |
| } | |
| Future close() async { | |
| var dbClient = await db; | |
| return dbClient.close(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Food { | |
| Food.init(); | |
| int idMeal; | |
| String strCategory; | |
| String strMeal; | |
| String strMealThumb; | |
| int get id => idMeal; | |
| String get name => strMeal; | |
| String get thumb => strMealThumb; | |
| String get category => strCategory; | |
| set idFromString(String _idMeal) => this.idMeal = int.parse(_idMeal.toString()); | |
| set name(String _strMeal) => this.strMeal = _strMeal; | |
| set thumb(String _strThumb) => this.strMealThumb = _strThumb; | |
| set category(String _strCategory) => this.strCategory = _strCategory; | |
| Food(Map foodMap) { | |
| this.idMeal = int.parse(foodMap['idMeal'].toString()) ?? 0; | |
| this.strMeal = foodMap['strMeal'] ?? ""; | |
| this.strMealThumb = foodMap['strMealThumb'] ?? ""; | |
| this.strCategory = foodMap['strCategory'] ?? ""; | |
| } | |
| Map<String, dynamic> toMap() { | |
| Map<String, dynamic> foodMap = Map(); | |
| foodMap['id'] = this.idMeal; | |
| foodMap['idMeal'] = this.idMeal; | |
| foodMap['strMeal'] = this.strMeal; | |
| foodMap['strMealThumb'] = this.strMealThumb; | |
| if (this.strCategory != null) foodMap['strCategory'] = this.strCategory; | |
| return foodMap; | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:food_cookbook/entity/food.dart'; | |
| abstract class FoodRepoContract { | |
| static var tableName = "foodFavorite"; | |
| static var id = "id"; | |
| static var idMeal = "idMeal"; | |
| static var strMeal = "strMeal"; | |
| static var strMealThumb = "strMealThumb"; | |
| static var strCategory = "strCategory"; | |
| Future<int> addFavorite(Food food); | |
| Future<int> removeFavorite(int id); | |
| Future<bool> isFavorite(int id); | |
| Future<List<Food>> getAllFavorites(); | |
| Future<int> getCount(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:food_cookbook/entity/food.dart'; | |
| import 'package:food_cookbook/repo/food_repo_contract.dart'; | |
| import 'package:sqflite/sqflite.dart'; | |
| import 'package:sqflite/sqlite_api.dart'; | |
| class FoodRepoImpl implements FoodRepoContract { | |
| Database _db; | |
| var tableName = FoodRepoContract.tableName; | |
| var columnId = FoodRepoContract.idMeal; | |
| FoodRepoImpl(Database db) { | |
| this._db = db; | |
| } | |
| @override | |
| Future<int> addFavorite(Food food) async => await _db.insert( | |
| tableName, | |
| food.toMap(), | |
| conflictAlgorithm: ConflictAlgorithm.replace | |
| ); | |
| @override | |
| Future<int> removeFavorite(int id) async => await _db.delete( | |
| tableName, | |
| where: '$columnId = ?', | |
| whereArgs: [id] | |
| ); | |
| @override | |
| Future<List<Food>> getAllFavorites() async { | |
| var result = await _db.query(tableName); | |
| var foodFav = (result.length > 0) ? | |
| List.generate(result.length, (i) { | |
| return Food(result[i]); | |
| }) : List<Food>(); | |
| return foodFav; | |
| } | |
| @override | |
| Future<int> getCount() async => Sqflite.firstIntValue(await _db.rawQuery( | |
| 'SELECT COUNT(*) FROM $tableName') | |
| ); | |
| @override | |
| Future<bool> isFavorite(int id) async { | |
| var result = await _db.query(tableName, | |
| where: '$columnId = ?', | |
| whereArgs: [id]); | |
| return result.length > 0; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| _getFavList() async { | |
| _foodFav.clear(); | |
| _databaseHelper = DatabaseHelper(); | |
| var _db = await _databaseHelper.db; | |
| _foodRepoImpl = FoodRepoImpl(_db); | |
| _foodRepoImpl.getAllFavorites().then((_favList) { | |
| _foodFav.addAll(_favList); | |
| }); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| dependencies: | |
| flutter: | |
| sdk: flutter | |
| # The following adds the Cupertino Icons font to your application. | |
| # Use with the CupertinoIcons class for iOS style icons. | |
| http: ^0.12.0+1 | |
| sqflite: ^1.1.6 | |
| path: ^1.6.4 | |
| async_loader: ^0.1.2 | |
| json_annotation: ^2.0.0 | |
| dev_dependencies: | |
| flutter_driver: | |
| sdk: flutter | |
| flutter_test: | |
| sdk: flutter | |
| flutter_launcher_icons: ^0.7.3 | |
| build_runner: ^1.7.1 | |
| json_serializable: ^2.0.0 | |
| test: ^1.6.0 | |
| mockito: ^4.1.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment