【问题标题】:Flutter BLoC authenticationFlutter BLoC 认证
【发布时间】:2021-08-01 16:10:31
【问题描述】:

我正在为我的移动应用程序使用 Flutter BLoC,但在我的 login_form.dart 中出现错误

return TextField(
          key: const Key('loginForm_emailInput_textField'),
          onChanged: (email) => context.bloc<LoginCubit>().emailChanged(email),
          focusNode: focusNode,
          keyboardType: TextInputType.emailAddress,
          decoration: InputDecoration(
            labelText: 'Email',
            helperText: '',
            errorText: state.email.invalid ? 'Invalid Email' : null,
          ),
        );

在这个TextField 区域中:

context.bloc<LoginCubit>().emailChanged(email),

.bloc 显示以下错误

lib/screens/login/view/login_form.dart:109:44: Error: The method 'bloc' isn't defined for the class 'BuildContext'.
     - 'BuildContext' is from 'package:flutter/src/widgets/framework.dart' ('../../../Desktop/flutter/packages/flutter/lib/src/widgets/framework.dart').
    Try correcting the name to the name of an existing method, or defining a method named 'bloc'.
              onChanged: (password) => context.bloc<LoginCubit>().passwordChanged(password),

有人可以提出解决方案吗?

【问题讨论】:

    标签: flutter mobile bloc


    【解决方案1】:

    context.bloc 已被弃用。 您可以阅读示例并进行必要的更改。

    另一种方法是使用BlocProvider 你可以这样使用它:

    BlocProvider.of<BlocName>(context).eventCall()
    

    【讨论】:

    • onChanged:(email) => context.bloc().emailChanged(email),应该怎么写呢? @MaddyB
    • 在此之前,我有一个问题,LoginCubit 是您的 Bloc Class 的名称还是用于初始化 bloc 的肘位?比如,LoginCubit 的类型是什么?
    • 如果是你的 Bloc 类,那么它可以被挂起:(email) => BlocProvider.of(context).email hanged(email)
    【解决方案2】:
    context.bloc<LoginCubit>().emailChanged(email),
    

    应该写成

    context.read<LoginCubit>().emailChanged(email),
    

    【讨论】:

      猜你喜欢
      • 2021-03-19
      • 2021-03-15
      • 2020-05-06
      • 2019-08-05
      • 2020-09-28
      • 2020-02-06
      • 2021-09-20
      • 2020-03-26
      • 2021-05-17
      相关资源
      最近更新 更多