OpenFlutter/Flutter-Notebook

Another exception was thrown: NoSuchMethodError: The getter 'bLoC' was called on null.

jp1017 opened this issue · 5 comments

使用的scope的bloc,

class BlocProvider extends InheritedWidget{
  final CountBloc bLoC = CountBloc();

  BlocProvider({Key key, Widget child}) : super(key: key, child: child);

  @override
  bool updateShouldNotify(_) => true;

  static CountBloc of(BuildContext context) =>
      (context.inheritFromWidgetOfExactType(BlocProvider) as BlocProvider).bLoC;
}

移植到自己项目报错:

I/flutter: Another exception was thrown: NoSuchMethodError: The getter 'bLoC' was called on null.

screenshot_2019-02-27-13-10-37-600_add2flutter

现在用的单例提供bloc

找到问题了,BlocProvider 要先调用

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      child: MaterialApp(
        title: 'scoped',
        theme: ThemeData.dark(),
        home: TopPage(),
      ),
    );
  }
}

After navigation I get the same error. Do I have to wrap my MaterialApp with BlocProvider?

HI guys, this kind of The getter 'id' was called on null. happens because of when you do HotReload.

To reproduce this

  1. Restart the app again(Check the app wether its working fine without doing any modification in hot-reload)
  2. If works fine, do some text or icon change and let hot-reload.
  3. Now do the testing again it will fail with 'The getter 'id' was called on null.'

I too waiting for the solution..

I am getting same error but while navigating to a page from a drawer. Below is the code but after number of attempts unable to find solution.

Navigator.push(context,MaterialPageRoute(builder: (context){
final nbloc = BlocProvider.of(context);
return BlocProvider(child: NotificationPage(), bloc: nbloc);
}));