[英]BlocProvider.of() called with a context that does not contain a Bloc of type CLASS
in flutter i just learn how can i use Bloc
on applications and i want to try to implementing simple login with this feature.在颤振中,我只是学习了如何在应用程序上使用
Bloc
,我想尝试使用此功能实现简单登录。 after implementing some class of bloc
to using that on view在实现某种类型的
bloc
以在视图上使用它之后
i get error when i try to use this code as当我尝试使用此代码时出现错误
BlocProvider.of<LoginListingBloc>(context).dispatch(LoginEvent(loginInfoModel: testLogin));
inside RaisedButton
内部
RaisedButton
Error:错误:
BlocProvider.of() called with a context that does not contain a Bloc of type LoginListingBloc.
BlocProvider.of() 使用不包含 LoginListingBloc 类型的 Bloc 的上下文调用。
My view :我的看法 :
class _HomePageState extends State<HomePage> {
LoginListingBloc _loginListingBloc;
@override
void initState() {
super.initState();
_loginListingBloc =
LoginListingBloc(loginRepository: widget.loginRepository);
}
...
@override
Widget build(BuildContext context) {
return BlocProvider(
bloc: _loginListingBloc,
child: Scaffold(
appBar: AppBar(
elevation: 5.0, title: Text('Sample Code', style: appBarTextStyle)),
body: Center(
child: RaisedButton(
child: Text(
'click here',
style: defaultButtonStyle,
),
onPressed: () {
BlocProvider.of<LoginListingBloc>(context).dispatch(LoginEvent(loginInfoModel: testLogin));
}),
),
),
);
}
}
LoginListingBloc
class: LoginListingBloc
类:
class LoginListingBloc extends Bloc<LoginListingEvent, LoginListingStates> {
final LoginRepository loginRepository;
LoginListingBloc({this.loginRepository});
@override
LoginListingStates get initialState => LoginUninitializedState();
@override
Stream<LoginListingStates> mapEventToState(
LoginListingStates currentState, LoginListingEvent event) async* {
if (event is LoginEvent) {
yield LoginFetchingState();
try {
final loginInfo = await loginRepository.fetchLoginToPage(
event.loginInfoModel.username, event.loginInfoModel.password);
yield LoginFetchedState(userInfo: loginInfo);
} catch (_) {
yield LoginErrorState();
}
}
}
}
and other classes if you want to see theme和其他课程,如果你想看主题
AppApiProvider
class: AppApiProvider
类:
class AppApiProvider {
final successCode = 200;
Future<UserInfo> fetchLoginToPage(String username, String password) async {
final response = await http.get(Constants.url + "/api/v1/getPersons");
final responseString = jsonDecode(response.body);
if (response.statusCode == successCode) {
print(responseString);
return UserInfo.fromJson(responseString);
} else {
throw Exception('failed to get information');
}
}
}
LoginEvent
: LoginEvent
:
class LoginEvent extends LoginListingEvent {
final LoginInfoModel loginInfoModel;
LoginEvent({@required this.loginInfoModel}) : assert(loginInfoModel != null);
}
LoginInfoModel
: LoginInfoModel
:
class LoginInfoModel {
String username;
String password;
LoginInfoModel({this.username, this.password});
}
final testLogin = LoginInfoModel(username:'exmaple',password:'text');
No need to access loginListingBloc from context
since it exists in the current class and not up the widget tree.无需从
context
访问 loginListingBloc,因为它存在于当前类中,而不是在小部件树上。
change:改变:
BlocProvider.of<LoginListingBloc>(context).dispatch(LoginEvent(loginInfoModel: testLogin));
to:到:
_loginListingBloc.dispatch(LoginEvent(loginInfoModel: testLogin));
For all others who come here for the error message: Make sure you always specify the types and don't omit them:对于所有其他人来这里查看错误消息:确保您始终指定类型并且不要省略它们:
BlocProvider<YourBloc>(
create: (context) => YourBloc()
child: YourWidget()
);
and also for也为了
BlocProvider.of<YourBloc>(context);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.