7

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?

Hamed
  • 5,867
  • 4
  • 32
  • 56
  • What do you need to accomplish ? Isn't a solution for you to access the blocA.state from BlocB constructor and treat this as a particular case ? This is a known breaking change (sadly) which was documented on the (Migration page](https://bloclibrary.dev/#/migration?id=%e2%9d%97bloc-does-not-emit-last-state-on-subscription). – jorjdaniel Nov 10 '20 at 18:53

1 Answers1

1

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.

Rod
  • 1,601
  • 12
  • 18
  • Just a completion to your answer: this breaking changes are stated in the [Migration to 6.0.0 Documentation](https://bloclibrary.dev/#/migration?id=%e2%9d%97bloc-does-not-emit-last-state-on-subscription). – jorjdaniel Nov 10 '20 at 18:44