Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bloc initial state is not emitted anymore

I have a bloc that listens to another bloc. After updating flutter_bloc package to version 6.0.2, the listener won't call anymore in the initial state.

class BlocA extends Bloc {
  final BlocB blocB = ...;

  ...

  blocA.blocB.listen((state) {
    DO SOMTTHING...
  });

  ...
}

I found this solution:

class BlocB extends Bloc<..., ...> with BehaviorSubjectBloc {
  ...
}

mixin BehaviorSubjectBloc<TEvent, TState> on Bloc<TEvent, TState> {
  @override
  StreamSubscription<TState> listen(
    void Function(TState state) onData, {
    Function onError,
    void Function() onDone,
    bool cancelOnError,
  }) {
    onData(this.state);

    return super.listen(
      onData,
      onError: onError,
      onDone: onDone,
      cancelOnError: cancelOnError,
    );
  }
}

I wonder is there any better solution?

like image 822
Hamed Avatar asked Aug 20 '20 11:08

Hamed


1 Answers

There is an issue about it in the flutter_bloc repo:
https://github.com/felangel/bloc/issues/1641

The issue is on the todo list.
You may fix your yaml to force the previous version, using single quotes and removing the "^" or any other symbols. Delete your ~/.pub-cache/... folder and your pubspec.lock file, run flutter packages get again and be happy.

Note that the pub-cache folder is shared among all your projects, so you may try to delete only the specific package inside it.
You can investigate transitive dependencies using flutter packages pub deps to see the flutter_bloc dependencies and delete them too.

like image 75
Rod Avatar answered Oct 20 '22 05:10

Rod