diff --git a/lib/src/form/src/controller.dart b/lib/src/form/src/controller.dart index dd6e4a6..cc88a76 100644 --- a/lib/src/form/src/controller.dart +++ b/lib/src/form/src/controller.dart @@ -235,7 +235,7 @@ class AstromicFormController extends FormController { return '${groupStructure.id}-#${firstSetOfFieldsWithID.length}-'; } - String removeInstanceFromFormGroup(String formGroupID, int indexToRemove, {bool isSubGroup = false}) { + void removeInstanceFromFormGroup(String formGroupID, int indexToRemove, {bool isSubGroup = false}) { // Get the group structure with the same ID FormGroupStructure? groupStructure; if (isSubGroup) { @@ -257,7 +257,7 @@ class AstromicFormController extends FormController { controllers.entries.where((MapEntry c) => RegExp(formGroupID + r'-#[\d+]-' + groupStructure!.fields.first).hasMatch(c.key)).nonNulls.toList(), ); - if (i >= firstSetOfFieldsWithID.length) { + if (indexToRemove >= firstSetOfFieldsWithID.length) { throw Exception('The index to remove is larger than the whole instances count.'); } else { // get the fields IDs @@ -267,35 +267,32 @@ class AstromicFormController extends FormController { List valuesIDs = groupStructure.values?.nonNulls.toList() ?? []; print('valueIDs: $valuesIDs'); - if (i == (firstSetOfFieldsWithID.length - 1)) { + if (indexToRemove == (firstSetOfFieldsWithID.length - 1)) { // Remove the last item for (String fieldID in groupStructure.fields) { - removeController('${groupStructure.id}-#$i-$fieldID'); + removeController('${groupStructure.id}-#$indexToRemove-$fieldID'); } if (groupStructure.values != null && groupStructure.values!.isNotEmpty) { for (String valueID in groupStructure.values!) { - removeController('${groupStructure.id}-#$i-$valueID'); + removeController('${groupStructure.id}-#$indexToRemove-$valueID'); } } } else { // Switch and remove - int nextIndex = i + 1; + int nextIndex = indexToRemove + 1; for (String fieldID in groupStructure.fields) { - set('${groupStructure.id}-#$i-$fieldID', value('${groupStructure.id}-#$nextIndex-$fieldID')); + set('${groupStructure.id}-#$indexToRemove-$fieldID', value('${groupStructure.id}-#$nextIndex-$fieldID')); removeController('${groupStructure.id}-#$nextIndex-$fieldID'); } if (groupStructure.values != null && groupStructure.values!.isNotEmpty) { for (String valueID in groupStructure.values!) { - set('${groupStructure.id}-#$i-$valueID', value('${groupStructure.id}-#$nextIndex-$valueID')); + set('${groupStructure.id}-#$indexToRemove-$valueID', value('${groupStructure.id}-#$nextIndex-$valueID')); removeController('${groupStructure.id}-#$nextIndex-$valueID'); } } } } - // Remove the controllers and values. - - return '${groupStructure.id}-#${firstSetOfFieldsWithID.length}-'; } // void removeFromFormGroup(String formGroupID, int index) {