1
I’m having a hard time filling one dropdown
. Using data from a query sqflite
I have already made the following codes:
A Future map that returns my database categories in the following format:
[
{
"_categoriaid":1,
"categorianome":"Investimentos"
},
{
"_categoriaid":2,
"categorianome":"Salário"
}
]
Code:
Future<Map> _categorias() async {
var db = await DatabaseHelper.instance.database;
List respostaCategoria = await db
.rawQuery('SELECT (_categoriaid),(categorianome) FROM categoria');
print(respostaCategoria);
return respostaCategoria[0];
}
For display I did this FutureBuiler
but the difficulty is time to fill the data in the dropdown.
From what I understand I need to convert this Map
returned from Future to a string and fill in the dropdown but I cannot do this conversion
FutureBuilder<Map>(
future: _categorias(),
builder: (context, snapshot) {
final state = snapshot.connectionState;
var resultado;
if(state == ConnectionState.done) {
if(snapshot.hasError) {
resultado = "Error";
} else {
resultado = snapshot.data;
if (resultado == null){
resultado = "Algo deu errado.";
}
}
}
return DropdownButton<String>(
items: resultado.map((String dropDownStringItem) {
return DropdownMenuItem<String>(
value: dropDownStringItem,
child: Text(dropDownStringItem),
);
}).toList(),
onChanged: (String newValueSelected){
setState(() {
this._currentItemSelected = newValueSelected;
});
},
value: _currentItemSelected,
isExpanded: true,
);
},
),
In the current form I get the following error:
The method 'map' was called on null.
Receiver: null
Tried calling: map(Closure: (String) => DropdownMenuItem<String>)
type '(String) => DropdownMenuItem<String>' is not a subtype of type '(String, dynamic) => MapEntry<dynamic, dynamic>' of 'transform'
What am I doing wrong?
I managed to make it work, thank you, I still do not understand some parts of the code I will study to understand.
– denis
If you need to answer your questions here, I will answer... Just one observation, the method
getData()
I did it just to simulate his comic book, you can ignore it.– Matheus Ribeiro