Future<>> no es un subtipo del tipo List.
Estoy obteniendo los datos de SQLite y tratando de establecerlos en un List view, pero estoy obteniendo un error:
type 'Future>' no es un subtipo de tipo 'List'
Aquí está mi función de consulta de SQLite:
Future> leerTodasLasNotas() async {
final db = await instancia.database;
final orderBy = '${NotaCampos.tiempoAgregado} ASC';
final resultado = await db?.query(tablaNotas, orderBy: orderBy);
return resultado!.map((json) => Nota.fromJson(json)).toList();
Y aquí es donde llamo a leerTodasLasNotas():
class WidgetListaNotas extends StatefulWidget {
const WidgetListaNotas({
Key? key,
}) : super(key: key);
@override
State createState() => _EstadoWidgetListaNotas();
}
class _EstadoWidgetListaNotas extends State {
@override
Widget build(BuildContext context) {
List notas = BaseDatosNotas.instance.leerTodasLasNotas();
return ListView.builder(
itemCount: notas.length,
itemBuilder: (ctx, index) {
return WidgetTarjetaNota(
cuerpo: notas[index].cuerpo,
titulo: notas[index].titulo,
tiempoAgregado:
'${notas[index].tiempoAgregado.day}/${notas[index].tiempoAgregado.month}/${notas[index].tiempoAgregado.year}',
id: notas[index].id,
);
});
}
}
davy.ai
El error que estás obteniendo se debe a que
readAllNotes()
devuelve unFuture
del tipoList<note>
, no unList<note>
directamente. Necesitas esperar a que elFuture
se complete y devuelva el resultado antes de intentar usarlo en tuListView.builder()
.Puedes hacer esto utilizando la palabra clave
await
dentro de una funciónasync
, de la siguiente manera:En esta implementación actualizada,
readNotes()
es una funciónasync
que espera a que elFuture
devuelto porreadAllNotes()
se complete, luego almacena la lista resultante de tipoList<notes>
en la variablenotes
y llama asetState()
para desencadenar una reconstrucción del árbol de widgets.En el método
build()
, simplemente podemos utilizar la variablenotes
directamente sin convertirla aList<note>
.