Flutter: How to handle dynamic forms with validation using FormBuilder?
I'm refactoring my project and I'm working through a tutorial and I'm trying to create a dynamic form in my Flutter application using the `flutter_form_builder` package (version 7.0.0). The form should allow users to add multiple entries of the same type, and I want to ensure that each entry is validated before submission. However, I'm facing an issue where the form validation doesn't seem to trigger properly for dynamically added fields. Hereβs a simplified version of my code where I'm trying to implement this: ```dart import 'package:flutter/material.dart'; import 'package:flutter_form_builder/flutter_form_builder.dart'; class DynamicFormExample extends StatefulWidget { @override _DynamicFormExampleState createState() => _DynamicFormExampleState(); } class _DynamicFormExampleState extends State<DynamicFormExample> { final GlobalKey<FormBuilderState> _fbKey = GlobalKey<FormBuilderState>(); List<Map<String, dynamic>> _formData = []; void _addEntry() { setState(() { _formData.add({"name": null}); }); } void _submit() { if (_fbKey.currentState.saveAndValidate()) { print(_fbKey.currentState.value); } else { print('Validation failed'); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Dynamic Form Example')), body: FormBuilder( key: _fbKey, child: Column( children: <Widget>[ ..._formData.map((entry) { return FormBuilderTextField( name: 'name_${_formData.indexOf(entry)}', decoration: InputDecoration(labelText: 'Name'), validator: FormBuilderValidators.required(context), ); }).toList(), ElevatedButton(onPressed: _addEntry, child: Text('Add Entry')), ElevatedButton(onPressed: _submit, child: Text('Submit')), ], ), ), ); } } ``` When I run the application and try to add multiple entries, the validation seems to only check the first entry correctly. Subsequent entries are not validated as expected, leading to the message 'Validation failed' even when valid data is entered. The printed output for `_fbKey.currentState.value` only shows the first entry. I have also tried calling `_fbKey.currentState.reset()` after adding a new entry, but that didn't resolve the issue. How can I properly manage the dynamic fields and ensure that validation works for all entries? Any advice or examples would be greatly appreciated! I've been using Dart for about a year now. I'm working in a Debian environment. Could someone point me to the right documentation?