English 中文(简体)
我们能够进入Bloc,而不通过建筑构造
原标题:Can we access Bloc without Passing Buildcontext param

I am migrating to GetX to Bloc, in GetController we don t need BuildContext to call controller, so is there any way to Provide the Context to the bloc class from BlocProvider and we get bloc without providing buildContext like for example:

BlocProvider(
create: (context) => TestBloc(context),
),
class TestBloc extends Bloc<TestEvent, TestState> {
  final BuildContext context;

  TestBloc(this.context) : super(const TestInitial(false)) {
    on<TestEvent1>((event, emit) {});
  }
  static TestBloc bloc() => BlocProvider.of<TestBloc>(context); //<--- Instance members can t be accessed from a static method.
// I want to access bloc without BuildContext
}

呼吁集团

final testBloc = TestBloc.bloc();

I need this because i want to navigate the user from notification tap and there we don t have

BuildContext
最佳回答
  1. Create the class. Here it named as NavigationService

    进口包装:面粉/材料。

     class NavigationService { 
       static GlobalKey<NavigatorState> navigatorKey = 
       GlobalKey<NavigatorState>();
     }
    
  2. 1. 确立航向 材料使用的主要财产。 d)

    Widget build(BuildContext context) { return MaterialApp( navigatorKey: NavigationService.navigatorKey, ) }

  3. 页: 1 现在,你可以使用你想要的任何地方,例如。

    a. 印刷(“-印刷”): (美元)

现在,您的项目如下:

 static TestBloc bloc() => BlocProvider.of<TestBloc>(NavigationService.navigatorKey.currentContext); 
问题回答

在“水稻”中,一般不建议使用植被树外的建筑群,因为它可能导致与植被生命周期有关的问题,而“水稻”框架则假设植被等级。 但是,如果你需要从非植被阶层获得建筑群,那么你可以考虑利用全球灰色或另一种机制将建筑群的内容传给这一类别。

www.un.org/Depts/DGACM/index_spanish.htm 就你的具体案例而言,如果你想要进入布洛卡,而不通过建筑工程,那么你可以考虑采用全球关键或静态的方法,以提及建筑工程。 这方面的一个例子是使用全球钥匙:。

import  package:flutter/material.dart ;
import  package:flutter_bloc/flutter_bloc.dart ;

class TestBloc extends Bloc<TestEvent, TestState> {
  TestBloc() : super(const TestInitial(false)) {
    on<TestEvent1>((event, emit) {});
  }

  static final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

  static TestBloc bloc() => BlocProvider.of<TestBloc>(navigatorKey.currentContext!);
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      navigatorKey: TestBloc.navigatorKey,
      home: BlocProvider(
        create: (context) => TestBloc(),
        child: MyHomePage(),
      ),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            final testBloc = TestBloc.bloc();
            // Use testBloc here
          },
          child: Text( Access Bloc ),
        ),
      ),
    );
  }
}

在这种例子中,NavigatorKey被用于提及建筑工程。 这一钥匙在材料应用中设定,然后,你可以将其用于在测试Bloc类中获取建筑工程。

Keep铭记,应当谨慎地利用全球钥匙或静态方法进入建筑群,而且你需要认识到与挥发性块中的植被生命周期相关的潜在陷阱。





相关问题
Flutter App cannot be installed on Android TV

I m building a Flutter app that should support Android TV and Mobile devices. Despite Google Play shows that it is supported, I cannot install app on my MiBox device. While trying to install it, both ...

Moving the icon button to the right causes rendering issues

I am trying to align the icon button to the right of the Text field, I tried the row which made the icon button not centered vertically and now I am trying out the Stack widget. Here is my code - ...