【问题标题】:Not able to Listen New Value From Bloc Flutter无法从 Bloc Flutter 中听到新的价值
【发布时间】:2020-03-28 07:40:09
【问题描述】:

我正在使用 flutter_bloc 库来管理应用程序的状态,因为我在 bloc 类中按下不同类状态更改的按钮,但无法在 build 方法中侦听 BlocProvider。

BlocBuilder<AppBarBloc, bool>(
          builder: (context, isEnabled) {
            print('state detected');
            return Container();
          },)

//in main.dart were providing the Blocs


MultiBlocProvider(
      providers: [
        BlocProvider<PagesBloc>(
          builder: (BuildContext context) => PagesBloc(),
        ),
        BlocProvider<AppBarBloc>(
          builder: (BuildContext context) => AppBarBloc(),
        ),
      ],
      child: Column(
        children: <Widget>[
          Expanded(
            child: MaterialApp(
              title: 'Flutter Demo',
              initialRoute: '/',
              onGenerateRoute: RouteGenerator.generateRoute,
              theme: ThemeData(
                primarySwatch: Colors.blue,
              ),
            ),
          ),
        ],
      ),
    );

【问题讨论】:

    标签: android ios flutter bloc flutter-bloc


    【解决方案1】:

    也许你可以查看这个关于bloc listener的链接。

    但只是作为你可以做的一个例子:

    BlocBuilder<CustomerRegisterBloc, CustomerRegisterState>
          buildCustomerRegisterOnState() {
        return BlocBuilder<CustomerRegisterBloc, CustomerRegisterState>(
          builder: (context, state) {
            if (state is InitialCustomerRegisterState) {
              return PersonalDataForm();
            }
            if (state is PasswordCustomerRegisterState) {}
            if (state is PaymentFormCustomerRegisterState) {}
            if (state is TermsCustomerRegisterState) {}
            return Container();
          },
        );
      }
    

    【讨论】:

      猜你喜欢
      • 2020-03-26
      • 2023-03-13
      • 2020-04-28
      • 2021-05-10
      • 2021-03-19
      • 1970-01-01
      • 2020-02-07
      • 2021-02-20
      • 1970-01-01
      相关资源
      最近更新 更多