I’m trying to select a value in a dropdown menu and transfer that value to the dropdown menu title when it closes, but an error message appears when I try to do this, but the value does appear in the dropdown menu title:
Unhandled Exception: A S2SingleSelection was used after being disposed.
Once you have called dispose() on a S2SingleSelection, it can no longer be used.
I don’t understand at any point I use S2SingleSelection.
Package used: https://pub.dev/packages/awesome_select version 6.0.0
Here is my code and the initial value:
String? _selectedLevel;
final firestore = FirebaseFirestore.instance;
SmartSelect.single(
selectedValue: _selectedLevel,
onChange: (newValue) {
setState(() {
_selectedLevel = newValue.value;
});
},
choiceItems: documents.map((DocumentSnapshot<dynamic> dataDoc) {
return S2Choice(
value: dataDoc.data()['field1'],
title: dataDoc.data()['field1'],
);
}).toList(),
placeholder: 'N/A',
tileBuilder: (context, value) {
return Container(
padding: EdgeInsets.symmetric(
vertical: MediaQuery.of(context).size.height * 0.01,
horizontal: MediaQuery.of(context).size.width * 0.03,
),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.onPrimary,
borderRadius: BorderRadius.circular(20),
),
child: Row(
children: [
Padding(
padding: EdgeInsets.symmetric(
horizontal: MediaQuery.of(context).size.width * 0.035,
),
child: Icon(
FontAwesomeIcons.locationDot,
color: Theme.of(context).colorScheme.primary,
size: MediaQuery.of(context).size.width * 0.065,
),
),
Expanded(
child: S2Tile(
value: Text(
_selectedLevel != null ? "" : "",
style: TextStyle(
color: Theme.of(context).colorScheme.primary,
fontSize: MediaQuery.of(context).size.width * 0.055,
),
),
onTap: () {
value.showModal();
},
title: Text(
_selectedLevel != null ? _selectedLevel! : "Choose",
style: TextStyle(
fontSize: MediaQuery.of(context).size.width * 0.055,
color: Theme.of(context).colorScheme.primary,
overflow: TextOverflow.ellipsis,
),
),
loadingText: "",
trailing: Icon(
FontAwesomeIcons.angleDown,
color: Theme.of(context).colorScheme.primary,
),
),
)
],
),
);
},
modalConfig: const S2ModalConfig(
title: '',
type: S2ModalType.bottomSheet,
),
choiceConfig: S2ChoiceConfig(
type: S2ChoiceType.radios,
style: S2ChoiceStyle(
showCheckmark: true,
color: Theme.of(context).colorScheme.primary,
titleStyle: TextStyle(
fontSize: MediaQuery.of(context).size.width * 0.055,
color: Theme.of(context).colorScheme.primary,
),
),
),
choiceBuilder: (context, item, isSelected) {
return Padding(
padding: EdgeInsets.symmetric(
vertical: MediaQuery.of(context).size.height * 0.015,
horizontal: MediaQuery.of(context).size.width * 0.03,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
GestureDetector(
onTap: () {
setState(() {
_selectedLevel = isSelected.value;
});
if (_selectedLevel != null) {
setState(() {
item.onModalClose(true);
item.closeModal();
});
}
},
child: Row(
children: [
Icon(
_selectedLevel == isSelected.value ? FontAwesomeIcons.solidCircleCheck : FontAwesomeIcons.circle,
color: Theme.of(context).colorScheme.primary,
size: MediaQuery.of(context).size.width * 0.055,
),
SizedBox(
width: MediaQuery.of(context).size.width * 0.05,
),
Text(
isSelected.value,
style: TextStyle(
overflow: TextOverflow.ellipsis,
fontSize: MediaQuery.of(context).size.width * 0.055,
color: Theme.of(context).colorScheme.primary,
),
),
],
),
),
Row(
children: [
GestureDetector(
onTap: () {
// TODO EDIT NAME SITE
},
child: Icon(
FontAwesomeIcons.penToSquare,
color: Theme.of(context).colorScheme.primary,
size: MediaQuery.of(context).size.width * 0.055,
),
),
SizedBox(
width: MediaQuery.of(context).size.width * 0.05,
),
GestureDetector(
onTap: () {
setState(() {
isSelected.value;
});
if (isSelected.value != null) {
CRUDMethodFirestore(
selectedLevel: isSelected.value,
itemModal: item,
context: context,
).delete();
}
setState(() {
_selectedLevel = null;
});
},
child: Icon(
FontAwesomeIcons.trash,
color: Theme.of(context).colorScheme.primary,
size: MediaQuery.of(context).size.width * 0.055,
),
),
],
)
],
),
);
},
),
The problem appears during the execution of my two GestureDetector, at the level of the onTap the dropdown menu (modal) closes having defined the new value in my initial value variable _selectedLevel, it works the value is well transmitted but the error message appears in the console but does not influence the objective of my code.
If you could help me solve my error that would be great, it tells me that S2SingleSelection was used after being disposed except in no case I dispose it for me.
Thanks.
3
Answers
The error keeps popping up, my code claims that an S2SingleSelection was used after being disposed.
The problem comes from this piece of code:
Old code:
New code with ValueNotifier:
The problem comes from my method: item.onModalClose(true) ; The method works, the modal closes and initializes my value and displays it on the screen, but it generates my dispose() error. If I delete this one and leave only the method item.closeModal(), this one gets my value, closes the modal but does not initialize the value to be displayed on the screen, it stays on the old value or the predefined value if no other value is selected.
Do 2 corrections one is onChange add newValue.value.toString() and other one is don’t use set state in onChange method. Rather than use ValueNotifier
I had the same problem and I managed to solve it. Below is how I solved the problem.