Estoy consultando a mi api para mostrar datos en una aplicación, pero aun no entiendo muy bien como se construyen los widgets y parece que el fallo viene por ahí, no me muestra dato alguno.
Mi código:
class LocalSinConexion extends StatefulWidget {
@override
_LocalSinConexion createState() => _LocalSinConexion();
}
class _LocalSinConexion extends State<LocalSinConexion> {
List<Locales> data = <Locales>[];
Future<List<Locales>> tomar_datos() async{
final arguments = (ModalRoute.of(context)?.settings.arguments ?? <String, dynamic>{}) as Map;
var slug = arguments['slug'];
var url = 'http://miweb/api/local-sin-conexion/$slug?api_key=key_cur_prod_fnPqT5xQEi5Vcb9wKwbCf65c3BjCCcCC';
final response = await http.get(Uri.parse(url));
var datos = jsonDecode(response.body);
var registros = <Locales>[];
registros.add(Locales.fromJson(datos));
print(datos);
return registros;
}
@override
void initState() {
super.initState();
tomar_datos().then((value){
setState(() {
data.addAll(value);
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Esto es el local'),
),
body: Column(
children: [
Expanded(
child: ListView.builder(
itemCount: data.length,
itemBuilder: (BuildContext context, int index){
return Container(
padding: EdgeInsets.all(15),
child: Text(
data[index].nombre
),
);
}
),
)
],
)
);
}
}
La consola de AndroidStudio me devuelve lo siguiente.
[VERBOSE-2:ui_dart_state.cc(198)] Unhandled Exception: dependOnInheritedWidgetOfExactType<_ModalScopeStatus>() or dependOnInheritedElement() was called before _LocalSinConexion.initState() completed. When an inherited widget changes, for example if the value of Theme.of() changes, its dependent widgets are rebuilt. If the dependent widget's reference to the inherited widget is in a constructor or an initState() method, then the rebuilt dependent widget will not reflect the changes in the inherited widget. Typically references to inherited widgets should occur in widget build() methods. Alternatively, initialization based on inherited widgets can be placed in the didChangeDependencies method, which is called after initState and whenever the dependencies change thereafter.
En si el código no me da ningún error pero parece que comenta que al inicializar el widget con initState()
no esta bien, o algo así comprendo yo.
También decir, que la consulta a la API trae siempre un único registro ya que solo traigo los datos del usuario que coincidan con el slug, posiblemente eso también lo este haciendo mal, adapte un código que tengo igual pero que si hacia falta hacer un for
.
Parece que el fallo esta en esto:
final arguments = (ModalRoute.of(context)?.settings.arguments ?? <String, dynamic>{}) as Map;
Comenta que se llamo a esto antes de que se completara _LocalSinConexion.initState().
pero no se como solventarlo
Alguna sugerencia??