flutter - 使用不包含 CLASS 类型的 Bloc 的上下文调用 BlocProvider.of()
问题描述
在颤振中,我只是学习如何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');
解决方案
无需访问 loginListingBloc ,context
因为它存在于当前类中而不是小部件树中。
改变:
BlocProvider.of<LoginListingBloc>(context).dispatch(LoginEvent(loginInfoModel: testLogin));
到:
_loginListingBloc.dispatch(LoginEvent(loginInfoModel: testLogin));
推荐阅读
- typescript - 如何在函数打字稿函数中提取和推断类型参数
- javascript - 我在终端-> npm install --save express-handlebars 中运行了这个命令,但车把仍然不在我的依赖项中
- nestjs - NestJS:从Feed中获取时间戳和偏移量
- c++ - 使用 .*& 访问基类成员(访问 priority_queue 容器)
- c# - ASP.Net Core React.js 模板 Azure AD 身份验证和授权
- r - 在 finalfit 包中计算中位数和 IQR
- javascript - 如何禁用特定测试的笑话模拟?
- vue.js - 为什么我无法在 Vue.js 的新选项卡中打开任何文件
- android - 来自相机的图像选择器未发送到 API
- node.js - Mongoose 仍然使用 select: false 发送密码字段