The problem is simple: What is the correct way to search element in array ?
My code is
data = [{id: 1, descripcion: Asier}, {id: 2, descripcion: Pepe}]
estateSelected= data.firstWhere((dropdown)=>dropdown.id==1);
The error that return is
Bad state: no element
firstWhere method Null safetyReturns the first element that satisfies the given predicate test . Iterates through elements and returns the first to satisfy test . If no element satisfies test , the result of invoking the orElse function is returned. If orElse is omitted, it defaults to throwing a StateError.
To get an element at specific index from a List in Dart, call elementAt() method on this list and pass the index as argument. elementAt() method returns the element. If the index is out of range for the list, then elementAt() throws RangeError .
You have some errors, this should work:
var data = [{'id': 1, 'descripcion': 'Asier'}, {'id': 2, 'descripcion': 'Pepe'}];
var estateSelected = data.firstWhere((dropdown) => dropdown['id'] == 1);
print(estateSelected);
Fastest way to try is on dartpad
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With