Consumer not reacting to changes in ChangeNotifier in profile/release builds

Issue

I have a class which extends from ChangeNotifier, it manages the state of one widget:

  MainSection _section = MainSection.SETUP;

  MainSection get section => _section;

  set section(MainSection value) {
    _section = value;

    // some code

    notifyListeners();
  }

As I said I use that to change the state of a Widget:

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<MainBloc>.value(
      value: _bloc,
      child: Consumer<MainBloc>(builder: (context, bloc, child) {
        _bloc = bloc;
        var body;

        switch (_bloc.section) {
          case MainSection.SETUP:
            body = _widgetFactory.createSetupWidget();
            break;
          case MainSection.WAITING:
            body = Column(
              children: <Widget>[
                Expanded(
                  child: _widgetFactory.createWaitingWidget(),
                ),
                _getBottomBar()
              ],
            );
            break;

This mechanism worked OK since I updated the app to work with the last Flutter version. Now in debug mode it works OK in all cases, but in profile or release mode it doesn’t work at a specific point in the app, meaning that it’s working for some state changes but for a particular change doesn’t work. I don’t know what can be affecting that.

Why I say it’s not working: I change the variable, call notifyListeners() but the consumer doesn’t get notified.

I’m using provider dependency version 4.3.1

Flutter doctor:

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.17.5, on Linux, locale en_US.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[✓] Android Studio (version 4.0)
[!] IntelliJ IDEA Community Edition (version 2019.2)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
[!] VS Code (version 1.47.3)
    ✗ Flutter extension not installed; install from
      https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter
[✓] Connected device (1 available)

Solution

I already discovered what happened, some child widget was handling a future in the build method:

  @override
  Widget build(BuildContext context) {
    return FutureProvider<FutureBundle>.value(
        value: _bloc.getChannels(),
        initialData: FutureBundle(state: BundleState.LOADING),
        catchError: (context, error) {
          return FutureBundle(state: BundleState.ERROR, data: error);
        },
        child: Consumer<FutureBundle>(builder: (context, bundle, view) {

I changed this implementation following this response reference and everything worked again:

@override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: future,
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          return ChangeNotifierProvider<WaitingBloc>.value(
              value: _bloc,
              child: Consumer<WaitingBloc>(builder: (context, bloc, child) {

Answered By – svprdga

Answer Checked By – Terry (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *