我正在为我的移动应用程序使用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),有没有人能给出一个解决方案。
发布于 2021-05-12 14:05:50
context.bloc<LoginCubit>().emailChanged(email),应该写成
context.read<LoginCubit>().emailChanged(email),发布于 2021-05-11 17:57:41
context.bloc已被弃用。您可以阅读示例并进行必要的更改。
另一种方法是使用BlocProvider,您可以这样使用它:
BlocProvider.of<BlocName>(context).eventCall()https://stackoverflow.com/questions/67484279
复制相似问题