Bloc: BlocListener doesn't fire after coming back to the view

Created on 20 Jun 2019  ·  3Comments  ·  Source: felangel/bloc

Describe the bug
I have a BlocListener on FirstPage from where I navigate to SecondPage. After navigating back from SecondPage to FirstPage BlocListener doesn't trigger anymore.

To Reproduce
Steps to reproduce the behavior:

  1. Clone https://github.com/korzonkiee/flutter_bloc_listener_issue
  2. Run flutter project.
  3. Click "Second page" button.
  4. Click "Back" button.
  5. Click "Second page" button again.
  6. See error - navigation is not triggered.

Expected behavior
BlocListener should trigger.

Flutter doctor output

[✓] Flutter (Channel stable, v1.5.4-hotfix.2, on Mac OS X 10.14.5 18F132, locale en-PL)
    • Flutter version 1.5.4-hotfix.2 at /Users/korzonkiee/Development/flutter
    • Framework revision 7a4c33425d (7 weeks ago), 2019-04-29 11:05:24 -0700
    • Engine revision 52c7a1e849
    • Dart version 2.3.0 (build 2.3.0-dev.0.5 a1668566e5)


[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at /Users/korzonkiee/Library/Android/Sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-28, build-tools 28.0.3
    • ANDROID_HOME = /Users/korzonkiee/Library/Android/Sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)
    • All Android licenses accepted.

[✓] iOS toolchain - develop for iOS devices (Xcode 10.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 10.1, Build version 10B61
    • ios-deploy 1.9.4
    • CocoaPods version 1.6.0

[!] Android Studio (version 3.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)

[!] IntelliJ IDEA Ultimate Edition (version 2018.3.5)
    • IntelliJ at /Applications/IntelliJ IDEA.app
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
    • For information about installing plugins, see
      https://flutter.dev/intellij-setup/#installing-the-plugins

[✓] VS Code (version 1.35.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.1.0

[✓] Connected device (1 available)
    • iPhone 6s • D5D8CABC-B92F-4FD5-B614-4687BEEFA846 • ios • iOS 12.1 (simulator)

! Doctor found issues in 2 categories.

Additional context

  • Testing on iOS simulator
  • Version of flutter_bloc library is 0.17.0
question

Most helpful comment

Hi @korzonkiee 👋
Thanks for opening an issue!

As @hawkinsjb1 mentioned, if you're not navigating in response to a state change, then the recommended approach is to just do that navigation in the onTap.

In your example, you can just update FirstState to not extend Equatable like so:

abstract class FirstState {}

class InitialFirstState extends FirstState {}

class ButtonClicked extends FirstState {}

You will then be able to yield new instances of ButtonClicked and they will be treated as different states giving you the desired behavior.

Hope that helps and great question 👍

All 3 comments

I've debugged the code execution and it turned out that BlocListener wasn't triggered because currentState of FirstBloc was the same as nextState. State change was not propagated because of this if-statement.

https://github.com/felangel/bloc/blob/6eeb358201611eb4be6460c91df582070bfb92d5/packages/bloc/lib/src/bloc.dart#L130

@felangel Could you suggest an idea of how to gracefully handle such behavior? Should I update bloc's state with some auxiliary state when leaving the page?

It isn't suggested to use bloc explicitly for navigation events, so avoiding the issue entirely with direct navigation is going to be your cleanest solution. With that being said, an easy way to handle your problem is something like this
if (event is ButtonClick) { yield ButtonClicked(); yield InitialFirstState(); }
so you're never 'stuck' on a navigation state

Hi @korzonkiee 👋
Thanks for opening an issue!

As @hawkinsjb1 mentioned, if you're not navigating in response to a state change, then the recommended approach is to just do that navigation in the onTap.

In your example, you can just update FirstState to not extend Equatable like so:

abstract class FirstState {}

class InitialFirstState extends FirstState {}

class ButtonClicked extends FirstState {}

You will then be able to yield new instances of ButtonClicked and they will be treated as different states giving you the desired behavior.

Hope that helps and great question 👍

Was this page helpful?
0 / 5 - 0 ratings