barely functional database with hive
This commit is contained in:
29
lib/services/providers/db/dbhelper.dart
Normal file
29
lib/services/providers/db/dbhelper.dart
Normal file
@@ -0,0 +1,29 @@
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
import '../../../models/recipe.dart';
|
||||
import '../../../example_data.dart' as e;
|
||||
|
||||
class DbHelper {
|
||||
static Box<Recipe> get _recipesBox => Hive.box(name: 'recipes');
|
||||
|
||||
static Future<void> init() async {
|
||||
final dir = await getApplicationDocumentsDirectory();
|
||||
Hive.defaultDirectory = dir.path;
|
||||
Hive.registerAdapter('Recipe', Recipe.fromJson);
|
||||
_recipesBox.clear();
|
||||
for (final recipe in e.exampleRecipes) {
|
||||
insertRecipe(recipe);
|
||||
}
|
||||
}
|
||||
|
||||
static List<Recipe> fetchRecipes() {
|
||||
List<Recipe> recipes = _recipesBox.getAll(['0', '1']).cast<Recipe>();
|
||||
|
||||
return recipes;
|
||||
}
|
||||
|
||||
static void insertRecipe(Recipe recipe) {
|
||||
_recipesBox.put(_recipesBox.length.toString(), recipe);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:rezepte/services/providers/db/dbhelper.dart';
|
||||
|
||||
import '../../models/recipe.dart';
|
||||
import 'package:rezepte/example_data.dart' as e;
|
||||
@@ -12,7 +13,7 @@ class RecipeListProvider extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
List<Recipe> get recipes => _recipes;
|
||||
List<Recipe> get recipes => DbHelper.fetchRecipes();
|
||||
|
||||
void clearRecipes({silent = false}) {
|
||||
_recipes.clear();
|
||||
|
||||
Reference in New Issue
Block a user