todolist watches for changes on db
This commit is contained in:
@@ -18,4 +18,7 @@ class DbHelper {
|
||||
static List<Todo> fetchTodos() => _isar.todos.where().findAll();
|
||||
|
||||
static void addTodo(Todo todo) => _isar.write((isar) => isar.todos.put(todo));
|
||||
|
||||
static Stream<void> watchTodos() =>
|
||||
_isar.todos.watchLazy(fireImmediately: true);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../models/todo.dart';
|
||||
import '../services/dbhelper.dart';
|
||||
|
||||
class TodoList extends StatefulWidget {
|
||||
@@ -10,7 +11,17 @@ class TodoList extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _TodoListState extends State<TodoList> {
|
||||
final _todos = DbHelper.fetchTodos();
|
||||
List<Todo> _todos = DbHelper.fetchTodos();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
DbHelper.watchTodos().listen((event) {
|
||||
setState(() {
|
||||
_todos = DbHelper.fetchTodos();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
Reference in New Issue
Block a user