1 year ago
#387316
Daniel Roldán
How to re-trigger initial event in Bloc instead using initState?
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
flutter
dart
bloc
0 Answers
Your Answer