flutterdartstack-overflowflutter-textformfieldflutter-form-builder

The following StackOverflowError was thrown while calling onSubmitted for TextInputAction.done


I am doing form saving with flutter 3.3.6

the code in the build method is:

TextFormField(
                      decoration: const InputDecoration(labelText: 'Image URL'),
                      textInputAction: TextInputAction.done,
                      keyboardType: TextInputType.url,
                      controller: _imageUrlController,
                      focusNode: _imageUrlFocusNode,
                      onSaved: (value) {
                        _editedProduct = Product(
                          id: _editedProduct.id,
                          title: _editedProduct.title,
                          description: _editedProduct.description,
                          price: _editedProduct.price,
                          imageUrl: value as String,
                        );
                      },
                      onFieldSubmitted: (_) {
                        if(_formKey.currentState!.validate()){
                        _formKey.currentState!.save();

                        }
                      },
                      validator: (value) {
                        if (value == null || value.isEmpty) {
                          return 'Enter a URL';
                        }
                        if ((value.startsWith('http://') &&
                                value.startsWith('https://')) ||
                            (value.endsWith('.jpg') &&
                                value.endsWith('.png') &&
                                value.endsWith('.jpeg'))) {
                          return 'Enter a valid URL';
                        }
                        return null;
                      },
                    ),

when saving the form the following error gets thrown:

The following StackOverflowError was thrown while calling onSubmitted for TextInputAction.done:
Stack Overflow

When the exception was thrown, this was the stack
#0      new _GrowableList (dart:core-patch/growable_array.dart:96:3)
#1      String._splitWithCharCode (dart:core-patch/string_patch.dart:1022:27)
#2      String.split (dart:core-patch/string_patch.dart:1042:16)
#3      _EditProductScreenState.build.<anonymous closure>
package:shop_app/screens/edit_product_screen.dart:195
#4      FormFieldState._validate
package:flutter/…/widgets/form.dart:398
#5      FormFieldState.validate.<anonymous closure>
package:flutter/…/widgets/form.dart:391
#6      State.setState
package:flutter/…/widgets/framework.dart:1114
#7      FormFieldState.validate
package:flutter/…/widgets/form.dart:390
#8      FormState._validate
package:flutter/…/widgets/form.dart:192
#9      FormState.validate
package:flutter/…/widgets/form.dart:186
#10     _EditProductScreenState.build.<anonymous closure>
package:shop_app/screens/edit_product_screen.dart:132
#11     FormFieldState.save
package:flutter/…/widgets/form.dart:369

knoing that when I comment out _formKey,currentState.save() the code runs normaly but without saving the form fields.

I tried to comment out the codelines that I suspect them


Solution

  • I discovered that I accedently called _formKey.currentState!.save() in an onsaved functions of one of my TextFormField's which in turn fires a recursion that overflowed the stack.