I have implemented a separate screen to show the error message. In the init state of the error screen, I have implemented a timer so that it pops back to the original login screen after 3 seconds of showing the error. However, after showing the screen for 3 seconds, I get a black screen, and not the original login screen. Can someone explain why? Is there a better way to do it?
Codes:
Widget build(BuildContext context) {
return BlocBuilder<AuthCubit, AuthState>(
builder: (context, state) {
if (state is AuthSuccess) {
return buildSuccessScreen(state);
} else if (state is AuthError) {
return ErrorScreen(state.message);
} else if (state is AuthPrompt) {
return _LoginForm(buildSuccessScreen);
} else {
return const Scaffold(
body: Center(child: CircularProgressIndicator()),
);
}
},
);
}
class ErrorScreen extends StatefulWidget {
const ErrorScreen(this.error, {super.key});
final String error;
@override
State<ErrorScreen> createState() => _ErrorScreenState();
}
class _ErrorScreenState extends State<ErrorScreen> {
@override
void initState() {
Timer(const Duration(seconds: 3), () {
Navigator.pop(context);
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey,
body: Center(
child: Text(
widget.error,
style: const TextStyle(
color: Colors.black,
fontSize: 20,
),
)),
);
}
}
2
Answers
Are you doing any
Navigator.of(context).push
? If not maybe you should update yourAuthCubit
‘s state after 3 seconds so yourbuild
method returns the correct widget (I believe_LoginForm(buildSuccessScreen)
)You are getting a black screen because you are actually not pushing anything on the navigation stack and simply just replacing screen based on your
AuthState
and then you are trying to pop the only screen available in navigation stack, thus you are seeing a blank screen.Your code:
Solution:
Please find implementation below:
If you find this useful, Kindly upvote this answer