Flutter BLoC 认证

Flutter BLoC authentication

我正在为我的移动应用程序使用 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),

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

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

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

BlocProvider.of<BlocName>(context).eventCall()
context.bloc<LoginCubit>().emailChanged(email),

应该写成

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