-
How to change the field value programmatically? Looks like the state is updated but the field is still rendered with the old value. Am I doing something wrong? class FormBloc extends Bloc<FormEvent, FormState> {
final GlobalKey<FormBuilderState> formKey;
FormBloc({required this.formKey}) : super(FormInitial()) {
on<Submit>((event, emit) {
print(formKey.currentState?.fields['email']?.value); // prints old value
formKey.currentState?.fields['email']?.setValue('newValue');
print(formKey.currentState?.fields['email']?.value); // prints new value, but the old value is still rendered
});
}
} class FormView extends StatelessWidget {
@override
Widget build(BuildContext context) {
var bloc = context.read<FormBloc>();
return Scaffold(
appBar: AppBar(title: Text('TestForm')),
body: FormBuilder(
key: bloc.formKey,
child: Column(
children: [
FormBuilderTextField(
key: UniqueKey(),
name: 'email',
decoration: InputDecoration(labelText: 'Email', border: OutlineInputBorder()),
),
SizedBox(height: 20),
FormBuilderTextField(
key: UniqueKey(),
name: 'password',
decoration: InputDecoration(labelText: 'Password', border: OutlineInputBorder()),
),
SizedBox(height: 20),
TextButton(
child: Text('Change Email'),
onPressed: () {
bloc.add(Submit());
},
)
],
),
),
);
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
andr2k
Jun 12, 2023
Replies: 1 comment
-
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
deandreamatias
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
didChange
is the answer, please disregard :)