Accessing FormBuilderState currentState value within dispose
#1260
-
I'm using Flutter Form Builder in order to collect data, then save to my database using Isar. When a user pops the widget either with the OS dependent "back button" or the "back button" on the AppBar, I want to store the state of the Form to a draft value. However, also I'm utilizing GoRouter, which doesn't work with class _MyWidgetState extends State<MyWidget> { final _formKey = GlobalKey<FormBuilderState>(); late Isar _isar; @override void initState() { super.initState(); _isar = context.read<IsarModel>().isar; } // I know this doesn't work, but this is ideally what I'd like to use @override void dispose() { if (_formKey.currentState?.value != null) { // write values from Form to Isar entry } } @override Widget build(BuildContext context) { return Scaffold( body: FormBuilder( key: _formKey, body: Column( // FormBuilderFields ) ); } } I can't get the form value because the widget has already popped from the tree and doesn't exist. I can't use the I've considered using |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi, sorry for delay response |
Beta Was this translation helpful? Give feedback.
Hi, sorry for delay response
Maybe you can use
onChange
property of FormBuilder to save values when changes.Remember to use
_formKey.currentState?.instantValue
instead_formKey.currentState?.value
, like I explain here.Also, the getters
_formKey.currentState?.isDirty
and_formKey.currentState?.isTouched
can be useful