Ad
Use Cubit For Flutter Form
I’m trying to use Cubit state management for a form (validation and submit). Is there any example I can follow?
I have already tried to implement it and it works, but every time there is an error, it shows me the form again with empty fields because the widget is repainted with initial data. How can I solve it?
Here my Cubit consumer code on the form:
BlocConsumer<LoginCubit, LoginState> buildLoginCard(BuildContext context) {
return BlocConsumer<LoginCubit, LoginState>(
listener: (context, state) {
if (state is LoginError) {
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text(state.message),
backgroundColor: Colors.red,
),
);
} else if (state is LoginSuccess) {
BlocProvider.of<AuthCubit>(context).autoLogin();
}
},
builder: (context, state) {
if (state is LoginInProgress) {
return LoadingWidget();
} else {
return LoginCardWidget();
}
},
);
}
Ad
Answer
you might try to add this to your BlocConsumer
after listener
buildWhen: (previous, current) {
if (current is LoginError)
return false;
else
return true;
},
in this case if the state is error it will not rebuild the widget agin and the fields will have there own values without being changed
Ad
source: stackoverflow.com
Related Questions
- → How do you create a 12 or 24 mnemonics code for multiple cryptocurrencies (ETH, BTC and so on..)
- → Flutter: input text field don't work properly in a simple example..... where am I wrong?
- → Can I customize the code formatting of Dart code in Atom?
- → Is it possible to develop iOS apps with Flutter on a Linux virtual machine?
- → Display SnackBar in Flutter
- → JSON ObjectMapper in Flutter
- → Material flutter app source code
- → TabBarSelection No such method error
- → How do I set the animation color of a LinearProgressIndicator?
- → Add different routes/screens to Flutter app
- → Is there a way to get the size of an existing widget?
- → How to share a file using flutter
- → Is there an easy way to find particular text built from RichText in a Flutter test?
Ad