Home > OS >  Can't use extension on BuildContext
Can't use extension on BuildContext

Time:07-21

I wrote this code to get access to my global scope more comfortable.

extension DepContextExtension on BuildContext {
  IGlobalDependency get global => read<IGlobalDependency>();
}

I had this code to use my global dependensy

final globalScope = context.read<IGlobalDependency>();

I change it to this

final globalScope = context.global();

but i got error, that method global isn't defined for the type BuildContext. What i did wrong and how i can fix this issue?

I get globalSopce inside WidgetModel Factory from library Elementary

MainAppScreenWidgetModel mainAppScreenWidgetModelFactory(BuildContext context) {
  final globalScope = context.global();

  return MainAppScreenWidgetModel(
    MainAppScreenModel(
      globalScope.baseBloc,
    ),
  );
}

this factory i put inside my Widget like this

class MainAppScreenWidget extends ElementaryWidget<MainAppScreenWidgetModel> {
  const MainAppScreenWidget({
    Key? key,
    WidgetModelFactory wmFactory = mainAppScreenWidgetModelFactory,
  }) : super(key: key, wmFactory);

CodePudding user response:

You've declared a property so to access it just use its name:

final globalScope = context.global;
  • Related