![](/img/trans.png)
[英]Flutter BLoC BlocProvider.of() called with a context that does not contain a Bloc of type
[英]BlocProvider.of() called with a context that does not contain a Bloc of type CLASS
在顫振中,我只是學習了如何在應用程序上使用Bloc
,我想嘗試使用此功能實現簡單登錄。 在實現某種類型的bloc
以在視圖上使用它之后
當我嘗試使用此代碼時出現錯誤
BlocProvider.of<LoginListingBloc>(context).dispatch(LoginEvent(loginInfoModel: testLogin));
內部RaisedButton
錯誤:
BlocProvider.of() 使用不包含 LoginListingBloc 類型的 Bloc 的上下文調用。
我的看法 :
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 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();
}
}
}
}
和其他課程,如果你想看主題
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
:
class LoginEvent extends LoginListingEvent {
final LoginInfoModel loginInfoModel;
LoginEvent({@required this.loginInfoModel}) : assert(loginInfoModel != null);
}
LoginInfoModel
:
class LoginInfoModel {
String username;
String password;
LoginInfoModel({this.username, this.password});
}
final testLogin = LoginInfoModel(username:'exmaple',password:'text');
無需從context
訪問 loginListingBloc,因為它存在於當前類中,而不是在小部件樹上。
改變:
BlocProvider.of<LoginListingBloc>(context).dispatch(LoginEvent(loginInfoModel: testLogin));
到:
_loginListingBloc.dispatch(LoginEvent(loginInfoModel: testLogin));
對於所有其他人來這里查看錯誤消息:確保您始終指定類型並且不要省略它們:
BlocProvider<YourBloc>(
create: (context) => YourBloc()
child: YourWidget()
);
也為了
BlocProvider.of<YourBloc>(context);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.