7

I'm using flutter_bloc for state management and landed on this issue. When updating a field and saving it, the BlocBuilder is not refreshing the page. It is working fine when Adding or Deleting. I'm not sure what I'm doing wrong here.

Even if I go to a different screen and returning to this screen it still displays the old data even though the file was updated.

I spent more than 2 hours trying to debug this to no avail. I tried initializing the updatedTodos = [] then adding each todo one by one, to see if that does something, but that didn't work either.

Any help here would be appreciated.

TodosBloc.dart:

Stream<TodosState> _mapUpdateTodoToState(
    TodosLoaded currentState,
    UpdateTodo event,
  ) async* {     
        if (currentState is TodosLoaded) {
        final index = currentState.Todos
            .indexWhere((todo) => event.todo.id == todo.id);
        final List<TodoModel> updatedTodos =
            List.from(currentState.todos)
              ..removeAt(index)
              ..insert(index, event.todo);

        yield TodosLoaded(updatedTodos);
        _saveTodos(updatedTodos);
      }
  }

todos_screen.dart:

...
Widget build(BuildContext context) {
    return BlocBuilder(
      bloc: _todosBloc,
      builder: (BuildContext context, TodosState state) {
        List<TodoModel> todos = const [];
        String _strings = "";
        if (state is TodosLoaded) {
          todos = state.todos;
        }
        return Expanded(
          child: ListView.builder(
            itemCount: todos.length,
            itemBuilder: (BuildContext ctnx, int index) {
              return Dismissible(
                key: Key(todo.toString()),
                child: DetailCard(
                  todo: todos[index],
                ),
              );
            },
          ),
        );
...

I'm expecting when the BlocBuilder to be called and refreshed the ListView.

Kajan Nallathamby
  • 287
  • 1
  • 2
  • 5

2 Answers2

20

I was able to resolve this with the help of Felix Angelov on github. The problem is that I'm extending Equatable but not passing the props to the super class in the TodoModel class. I had to update the constructor of the TodoModel with a super([]).

Slawa
  • 1,141
  • 15
  • 21
Kajan Nallathamby
  • 287
  • 1
  • 2
  • 5
  • 3
    I'm running into the exact same issue, but I'm not extending my model class with Equatable. I have no idea how to fix this. I really feel like I'm using flutter_bloc "correctly". Seems like a design flaw with this particular library. – Tyler Biscoe Aug 27 '19 at 02:42
1

This is the way i solved the issue , even though it could not be the best solution but i'll share it , when you are on the other screen where you are supposed to show data or something , upon pressing back button call dispose as shown below

 @override
 void initState() {
   super.initState();
   print("id" + widget.teamID);
   BlocProvider.of<FootBallCubit>(context).getCurrentTeamInfo(widget.teamID);
 }

 // what i noticed upon closing this instance of screen , it deletes old data
 @override
 void dispose() {
   super.dispose();
   Navigator.pop(context);
 }
Taki
  • 3,290
  • 1
  • 16
  • 41