如何在Bloc中重新触发初始事件而不是使用Initstate?
这就是事实,我将get_it用作依赖项注入,因此我有代码:
final sl = GetIt.instance;
Future<void> init() async {
sl.registerFactory(
() => ABloc(
services: sl(),
),
);
[...]
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
[...]
}
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
[...]
BlocProvider<ABloc>(
create: (context) =>
di.sl<ABloc>()..add(GetAInformation()), // trigger the event
),
],
child: MaterialApp...
);
}
}
当我从第一个屏幕(screenlogin)输入到具有信息(screeninfo)的屏幕(screeninfo)时,输入到bloc并获取信息,但是当我返回我的屏幕(ScreenLogin)使用PuspNamedAndRemoveuntil模拟“注销”,然后再次返回不再进入集团并触发事件,我想知道我在做错了什么或如何重新触发我的工作方式再次活动。
class ScreenInfo extends StatelessWidget{
// @override
// void initState() {
// super.initState();
// BlocProvider.of<ABloc>(context).add(const GetAInformation());
// }
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async {
Navigator.of(context).pushNamedAndRemoveUntil(
PageNames.screenLogin,
(Route<dynamic> route) => false,
},
return false;
},
child: Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(10),
child: Column(
children: <Widget>[
// BLOC
BlocBuilder<ABloc, AState>(
builder: (context, state) {
switch (state.status) {
case ProjectListStatus.success:
return Text(state);
case ProjectListStatus.failure:
return Text(state);
default:
return const LoadingWidget();
}
},
),
],
),
),
),
);
}
}
PS:我曾经进入Screeninfo Apphreplacement amendem namplacement
Here's the thing, I use get_it as dependency injection, and so I have the code:
final sl = GetIt.instance;
Future<void> init() async {
sl.registerFactory(
() => ABloc(
services: sl(),
),
);
[...]
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
[...]
}
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
[...]
BlocProvider<ABloc>(
create: (context) =>
di.sl<ABloc>()..add(GetAInformation()), // trigger the event
),
],
child: MaterialApp...
);
}
}
When I enter from the first screen (ScreenLogin), to my screen that has the information (ScreenInfo), enter to Bloc and gets the informacion, however when I return to my screen (ScreenLogin) simulating a "LogOut" with pushNamedAndRemoveUntil, and return again no longer enters to the Bloc and trigger the event, I would like to know what I'm doing wrong or how I do to re-trigger the event again.
class ScreenInfo extends StatelessWidget{
// @override
// void initState() {
// super.initState();
// BlocProvider.of<ABloc>(context).add(const GetAInformation());
// }
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async {
Navigator.of(context).pushNamedAndRemoveUntil(
PageNames.screenLogin,
(Route<dynamic> route) => false,
},
return false;
},
child: Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(10),
child: Column(
children: <Widget>[
// BLOC
BlocBuilder<ABloc, AState>(
builder: (context, state) {
switch (state.status) {
case ProjectListStatus.success:
return Text(state);
case ProjectListStatus.failure:
return Text(state);
default:
return const LoadingWidget();
}
},
),
],
),
),
),
);
}
}
PS : I use to enter ScreenInfo a pushReplacementNamed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论