modular icon indicating copy to clipboard operation
modular copied to clipboard

Help needed with binding a BLoC to a Module and right dispose it

Open saviogrossi opened this issue 5 years ago • 7 comments

I'm trying to do bind a BLoC to a ChildModule and use it with BlocBuilder above in a widget as suggeted here on docs. The problem is when I back from de second page and Modular disposes the CounterCubit BLoC, a exception above is throw. Looks like the SecondPage was rebuild after removal, and Modular.get fail. Did I miss something?

Stack trace

"Error: ModularError: CounterCubit not found
    at Object.throw_ [as throw] (http://localhost:64050/dart_sdk.js:4354:11)
    at modular_impl.ModularImpl.new.get (http://localhost:64050/packages/flutter_modular/src/presenters/widgets/widget_module.dart.lib.js:3053:17)
    at main.SecondPage.new.build (http://localhost:64050/packages/safezonne/main.dart.lib.js:893:47)
    at framework.StatelessElement.new.build (http://localhost:64050/packages/flutter/src/widgets/widget_span.dart.lib.js:48998:26)
    at framework.StatelessElement.new.performRebuild (http://localhost:64050/packages/flutter/src/widgets/widget_span.dart.lib.js:26056:22)
    at framework.StatelessElement.new.rebuild (http://localhost:64050/packages/flutter/src/widgets/widget_span.dart.lib.js:25210:12)
    at framework.StatelessElement.new.update (http://localhost:64050/packages/flutter/src/widgets/widget_span.dart.lib.js:49006:12)
    at framework.SingleChildRenderObjectElement.new.updateChild 
 [...]

Modular version 3.0.0-nullsafety.28.

Complete Code

void main() {
  runApp(ModularApp(module: AppModule()));
}

class AppModule extends MainModule {

  @override
  final List<Bind> binds = [];

  @override
  final List<ModularRoute>  routes = [
      ModuleRoute('/', module: HomeModule(), /* guards: [AuthenticationGuard()] */),      
      ModuleRoute('/second', module: SecondModule(), /* guards: [AuthenticationGuard()] */),      
  ];

  @override
  final Widget bootstrap = AppWidget();
}

class AppWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      initialRoute: "/",
    ).modular();
  }
}

class HomeModule extends ChildModule {

  @override
  final List<Bind> binds = [];

  @override
  final List<ModularRoute>  routes = [
      ChildRoute('/', child: (_, __) => HomePage()),
  ];

}

class HomePage extends StatelessWidget {
  const HomePage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('HomePage')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('HomePage'),
            RaisedButton(
              child: Text('Go to Second Page'),
              onPressed: () => Modular.to.pushNamed('/second')
            ),
          ],
        ),
      )
    );
  }
}

class SecondModule extends ChildModule {
  @override
  final List<Bind> binds = [
    Bind.singleton((i) => CounterCubit())
  ];

  @override
  final List<ModularRoute>  routes = [
      ChildRoute('/', child: (_, __) => SecondPage()),
  ];

}

class SecondPage extends StatelessWidget {
  const SecondPage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    CounterCubit counterCubit = Modular.get<CounterCubit>();

    return Scaffold(
      appBar: AppBar(title: const Text('SecondPage')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('SecondPage'),
            BlocBuilder(cubit: counterCubit, builder: (_, count) => Text("$count")),
            RaisedButton(
              child: Text('Go to Home Page'),
              onPressed: () {
                Modular.to.pop();
              },
            ),
          ],
        ),
      )
    );
  }
}

class CounterCubit extends Cubit<int> implements Disposable {
  CounterCubit() : super(0) {
    print("new CounterCubit");
  }
  void increment() => emit(state + 1);
  void decrement() => emit(state - 1);

  @override
  void dispose() {
    print("CounterCubit dispose() called, about to call close() now...");
    close();
  }
}

saviogrossi avatar Jan 12 '21 19:01 saviogrossi

I think this issue is solved now. See this commit which is merged 2 days ago: https://github.com/Flutterando/modular/commit/c27e342471286dcaad862b8c0fc9b0294fc2c461

pashtostories avatar Jan 14 '21 08:01 pashtostories

I think this issue is solved now. See this commit which is merged 2 days ago: c27e342

Hey @pashtostories - Unfortunaly no, I'm still experiencing this issue here, even after that merge. Btw, was me that submitted that PR but that was another issue prior this one. Still trying to figure out what I'm missing here.

saviogrossi avatar Jan 14 '21 14:01 saviogrossi

I have a very similar issue trying to get a Bloc instead of a Cubit. I'm using flutter_modular: 3.0.0-nullsafety.36

fabriziocacicia avatar Jan 22 '21 21:01 fabriziocacicia

ping @jacobaraujo7 - any thoughts?

saviogrossi avatar Jan 25 '21 22:01 saviogrossi

I'd like to give some more attention to this issue since this error prevents me to use the Flutter inspector, since every time I activate the Select Widget Mode on Android Studio the framework disposes for a moment the modules and the above-mentioned error occurs.

And this is still happening on the latest 3.0.0-nullsafety.45 version.

fabriziocacicia avatar Feb 23 '21 14:02 fabriziocacicia

i am tracking this error.

jacobaraujo7 avatar Feb 23 '21 17:02 jacobaraujo7

Check the new version! It's fixed! Thanks

jacobaraujo7 avatar Apr 11 '21 02:04 jacobaraujo7