class VincularUsuariosWidget extends StatefulWidget {
final List<Map<String, dynamic>> usuariosDisponiveis;
final List<Map<String, dynamic>> funcoesDisponiveis;
final Function(List<Map<String, int>>) onUsuariosVinculadosChanged;
final List<Map<String, dynamic>>? jaVinculados;
}
}, ajuste: Restarted application in 121ms.
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown building Builder(dirty, dependencies: [_FocusInheritedScope]):
Assertion failed:
file:///home/iapar/snap/flutter/common/flutter/packages/flutter/lib/src/material/dropdown.dart:1040:15
dropdown.dart:1040
items == null || items.isEmpty || value == null ||
items.where((DropdownMenuItem item) {
return item.value == value;
}).length == 1
"There should be exactly one item with [DropdownButtonFormField]'s value: 3. \nEither zero or 2 or
more [DropdownMenuItem]s were detected with the same value"
The relevant error-causing widget was:
DropdownButtonFormField
DropdownButtonFormField:file:///home/iapar/Documentos/fieldbook2/mock_field_book/lib/Screens/projeto/componentes_compartilhados/vincular_usuarios_widget.dart:78:9
When the exception was thrown, this was the stack:
dart-sdk/lib/internal/js_dev_runtime/private/ddc_runtime/errors.dart 296:3 throw
errors.dart:296
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 29:3 assertFailed
errors.dart:29
packages/flutter/src/material/dropdown.dart 1043:28 _formField
dropdown.dart:1043
packages/flutter/src/material/dropdown.dart 1733:25
dropdown.dart:1733
packages/flutter/src/widgets/basic.dart 7688:48 build
basic.dart:7688
packages/flutter/src/widgets/framework.dart 5687:22 build
framework.dart:5687
packages/flutter/src/widgets/framework.dart 5617:15 performRebuild
The exception you're encountering occurs because the value provided to the DropdownButtonFormField is no longer present in the list of items. This often happens when the selected value is removed from the list of items, causing a mismatch.
Understanding the Issue:
In your build method, you filter out the already linked users from the list of available users:
However, the setState method doesn't immediately rebuild the widget. Between the time setState is called and the widget is rebuilt, the DropdownButtonFormField still holds the old usuarioIdSelecionado value, which has just been removed from usuariosDisponiveisAtualizados. This causes the DropdownButtonFormField to have a value that isn't present in its items, leading to the assertion error:
code
"There should be exactly one item with [DropdownButtonFormField]'s value: 3. Either zero or 2 or more [DropdownMenuItem]s were detected with the same value."
Solution:
To resolve this issue, you need to ensure that the value provided to the DropdownButtonFormField is always present in the items list. You can achieve this by adjusting the value parameter in your DropdownButtonFormField to check if the current usuarioIdSelecionado exists in the updated list of users. If it doesn't, set it to null.
We create a local variable selectedUsuarioId that checks if the usuarioIdSelecionado exists in the updated list of available users. If it doesn't, we set it to null.
We then use selectedUsuarioId as the value for the DropdownButtonFormField instead of usuarioIdSelecionado.
dart
value: selectedUsuarioId,
This adjustment ensures that the DropdownButtonFormField always has a valid value that exists within its items, preventing the assertion error you're seeing.
Alternative Approach:
Another way to handle this is by resetting usuarioIdSelecionado before the build process begins after adding a user. However, since setting state variables during the build is not recommended, the approach above is cleaner and ensures the DropdownButtonFormField remains consistent with its items.
Summary:
The error occurs because the selected value is not present in the items of the DropdownButtonFormField.
Adjust the value property to ensure it matches an item in the items list or is null.
This prevents the assertion error and maintains the integrity of the widget's state.