Flutter Bloc: BlocBuilder not getting called after an update, ListView still displays old data
Asked Answered
S

2

8

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.

Slap answered 3/4, 2019 at 20:32 Comment(0)
S
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([]).

Slap answered 4/4, 2019 at 2:33 Comment(1)
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.Fagoting
P
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);
 }
Pacer answered 28/8, 2021 at 5:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.