I'm new in flutter, and I was trying to make a Dropdownbuttom item, where users can select one item from the list (which I take from FirebaseFirestore), and if the user takes that item, and save it using the button, that item won't be available for others users.
This is the method for the onPress() function from my save's button.
Future saveData(String uid, String enrollment) async {
// Limpiar la matricula donde aparezca el usuario.
final querySnapshot =
await FirebaseFirestore.instance.collection('Matriculas').get();
final enrollmentOld = querySnapshot.docs.firstWhere(
(doc) => doc['infoCoche']['idUser'] == uid,
orElse: () => null,
);
if (enrollmentOld != null) {
try {
FirebaseFirestore.instance
.collection('Matriculas')
.doc(enrollmentOld.id)
.update({
'infoCoche': {'idUser': uid, 'enUso': false}
});
} catch (e) {
print(e.toString());
}
}
//Update en la nueva matricula con el nuevo usuario
try {
FirebaseFirestore.instance
.collection('Matriculas')
.doc(enrollment)
.update({
'infoCoche': {'idUser': uid, 'enUso': true}
});
} catch (e) {
print(e.toString());
}
}
First I search if the user has already an enrollment selected, so if he wants to change to another one I can release the old one. If so, I put the bool enUso to false. Do the same for the item that he's selecting but true. If don't, I just ignore that part and change the new enrollment's attribute to true.
This is the DropDownButton.
var _currentItemSelected;
Widget _formSelectEnrollment() {
return StreamBuilder(
stream: FirebaseFirestore.instance.collection('Matriculas').snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(),
);
}
List<DocumentSnapshot> docs = snapshot.data.docs;
List<String> listEnrollment = List();
for (DocumentSnapshot ids in docs) {
if (!ids['infoCoche']['enUso']) {
listEnrollment.add(ids.id);
}
}
return Container(
margin: EdgeInsets.symmetric(vertical: 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Seleccionar matricula',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15),
),
SizedBox(
height: 10,
),
DropdownButtonFormField<String>(
items: listEnrollment.map((String dropDownStringItem) {
return DropdownMenuItem<String>(
value: dropDownStringItem,
child: Text(dropDownStringItem),
);
}).toList(),
validator: (value) => value == null
? 'Es necesario seleccionar una matricula'
: null,
onChanged: (String newValueSelected) {
setState(() {
_currentItemSelected = newValueSelected;
});
},
decoration: InputDecoration(
border: InputBorder.none,
fillColor: Color(0xfff3f3f4),
filled: true),
)
],
),
);
},
);
}
As you can see I'm using StreamBuilder(), so I get a list with all enrollments which are not in use. Also due to it's a streambuilder, if I change something in FireStore it will change in real time.
But, for some reason, which I'm not concern about it, every time I try to select a new enrollment, and save it, it returns the next error.
Performing hot reload...
Reloaded 4 of 838 libraries in 282ms.
I/flutter (17066): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (17066): The following assertion was thrown building Builder(dirty, dependencies: [_FocusMarker]):
I/flutter (17066): There should be exactly one item with [DropdownButton]'s value: 5816KNK.
I/flutter (17066): Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
I/flutter (17066): 'package:flutter/src/material/dropdown.dart':
I/flutter (17066): Failed assertion: line 834 pos 15: 'items == null || items.isEmpty || value == null ||
I/flutter (17066): items.where((DropdownMenuItem<T> item) {
I/flutter (17066): return item.value == value;
I/flutter (17066): }).length == 1'
I/flutter (17066):
I/flutter (17066): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter (17066): more information in this error message to help you determine and fix the underlying cause.
I/flutter (17066): In either case, please report this assertion by filing a bug on GitHub:
I/flutter (17066): https://github.com/flutter/flutter/issues/new?template=BUG.md
I/flutter (17066):
I/flutter (17066): The relevant error-causing widget was:
I/flutter (17066): DropdownButtonFormField<String>
I/flutter (17066): file:///C:/Users/Sharki/Documents/4.%20Proyecto%20flutter/Pruebas%20Flutter/Asereje/lib/paginas/accountSettings.dart:55:15
I/flutter (17066):
I/flutter (17066): When the exception was thrown, this was the stack:
I/flutter (17066): #2 new DropdownButton (package:flutter/src/material/dropdown.dart:834:15)
I/flutter (17066): #3 new DropdownButtonFormField.<anonymous closure>.<anonymous closure> (package:flutter/src/material/dropdown.dart:1525:27)
I/flutter (17066): #4 Builder.build (package:flutter/src/widgets/basic.dart:7183:48)
I/flutter (17066): #5 StatelessElement.build (package:flutter/src/widgets/framework.dart:4701:28)
I/flutter (17066): #6 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4627:15)
I/flutter (17066): #7 Element.rebuild (package:flutter/src/widgets/framework.dart:4343:5)
I/flutter (17066): #8 StatelessElement.update (package:flutter/src/widgets/framework.dart:4708:5)
I/flutter (17066): #9 Element.updateChild (package:flutter/src/widgets/framework.dart:3314:15)
I/flutter (17066): #10 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6125:14)
I/flutter (17066): #11 Element.updateChild (package:flutter/src/widgets/framework.dart:3314:15)
I/flutter (17066): #12 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4652:16)
I/flutter (17066): #13 Element.rebuild (package:flutter/src/widgets/framework.dart:4343:5)
I/flutter (17066): #14 ProxyElement.update (package:flutter/src/widgets/framework.dart:4987:5)
I/flutter (17066): #15 _InheritedNotifierElement.update (package:flutter/src/widgets/inherited_notifier.dart:183:11)
I/flutter (17066): #16 Element.updateChild (package:flutter/src/widgets/framework.dart:3314:15)
I/flutter (17066): #17 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4652:16)
I/flutter (17066): #18 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4800:11)
I/flutter (17066): #19 Element.rebuild (package:flutter/src/widgets/framework.dart:4343:5)
I/flutter (17066): #20 StatefulElement.update (package:flutter/src/widgets/framework.dart:4832:5)
I/flutter (17066): #21 Element.updateChild (package:flutter/src/widgets/framework.dart:3314:15)
I/flutter (17066): #22 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4652:16)
I/flutter (17066): #23 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4800:11)
I/flutter (17066): #24 Element.rebuild (package:flutter/src/widgets/framework.dart:4343:5)
I/flutter (17066): #25 StatefulElement.update (package:flutter/src/widgets/framework.dart:4832:5)
I/flutter (17066): #26 Element.updateChild (package:flutter/src/widgets/framework.dart:3314:15)
I/flutter (17066): #27 RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5647:32)
I/flutter (17066): #28 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6246:17)
I/flutter (17066): #29 Element.updateChild (package:flutter/src/widgets/framework.dart:3314:15)
I/flutter (17066): #30 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6125:14)
I/flutter (17066): #31 Element.updateChild (package:flutter/src/widgets/framework.dart:3314:15)
I/flutter (17066): #32 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4652:16)
I/flutter (17066): #33 Element.rebuild (package:flutter/src/widgets/framework.dart:4343:5)
I/flutter (17066): #34 StatelessElement.update (package:flutter/src/widgets/framework.dart:4708:5)
I/flutter (17066): #35 Element.updateChild (package:flutter/src/widgets/framework.dart:3314:15)
I/flutter (17066): #36 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4652:16)
I/flutter (17066): #37 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4800:11)
I/flutter (17066): #38 Element.rebuild (package:flutter/src/widgets/framework.dart:4343:5)
I/flutter (17066): #39 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2730:33)
I/flutter (17066): #40 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:913:20)
I/flutter (17066): #41 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:302:5)
I/flutter (17066): #42 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1117:15)
I/flutter (17066): #43 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1055:9)
I/flutter (17066): #44 SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:971:5)
I/flutter (17066): #48 _invoke (dart:ui/hooks.dart:251:10)
I/flutter (17066): #49 _drawFrame (dart:ui/hooks.dart:209:3)
I/flutter (17066): (elided 5 frames from class _AssertionError and dart:async)
I/flutter (17066):
I/flutter (17066): ════════════════════════════════════════════════════════════════════════════════════════════════════
As I said, I'm really new in flutter, also I'm sorry for my English.