5. Adding the logic of the BLoC

Next, still in the TodoBloc class, create the functions needed to implement the stream of data, starting with the method that will get the todos from the sembast database. The getTodos() returns a Future, and will await the result of db.Todos before updating the todos list:

Future getTodos() async {
List<Todo> todos = await db.getTodos();
todoList = todos;
todosSink.add(todos);
}

Also, create a function that just returns the todos list, calling it returnTodos:

 List<Todo> returnTodos (todos) {
return todos;
}

Finally, create the three methods needed to call the database methods to delete, update, and add a Todo. After calling each database function, call the getTodos() method to update the stream of data:

void _deleteTodo(Todo todo) {
db.deleteTodo(todo).then((result){
getTodos();
});
}
void _updateTodo(Todo todo) {
db.updateTodo(todo).then((result){
getTodos();
});
}
void _addTodo(Todo todo) {
db.insertTodo(todo).then((result) {
getTodos();
});
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.206.69