gpt4 book ai didi

flutter - 使用不包含 CLASS 类型的 Bloc 的上下文调用 BlocProvider.of()

转载 作者:IT王子 更新时间:2023-10-29 06:57:05 28 4
gpt4 key购买 nike

在 flutter 中,我刚刚学习了如何在应用程序上使用 Bloc,我想尝试使用此功能实现简单的登录。在实现一些 bloc 类以在 View 中使用它之后

当我尝试将此代码用作

BlocProvider.of<LoginListingBloc>(context).dispatch(LoginEvent(loginInfoModel: testLogin));

RaisedButton

错误:

BlocProvider.of() called with a context that does not contain a Bloc of type LoginListingBloc.

我的观点:

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');
}
}
}

登录事件:

class LoginEvent extends LoginListingEvent {
final LoginInfoModel loginInfoModel;

LoginEvent({@required this.loginInfoModel}) : assert(loginInfoModel != null);
}

登录信息模型:

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));

关于flutter - 使用不包含 CLASS 类型的 Bloc 的上下文调用 BlocProvider.of(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56097442/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com