This commit is contained in:
2025-04-08 12:03:27 +02:00
parent 52ac8f4ee3
commit ad3331764b

View File

@@ -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<String, String> 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<String> valuesIDs = groupStructure.values?.nonNulls.toList() ?? <String>[];
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) {