0
An error occurred that I don’t know how to fix in Flutter:
Rangeerror (index): Invalid value: Valid value range is Empty: 0
The idea is to load a profile page and this data is recovered with http.post
via JSON.
The problem is because the object does not exist until it finishes loading the JSON that comes via HTTP.
But I don’t know how to fix it. Follow my code:
class ProfilePage extends StatefulWidget {
@override
MapScreenState createState() => MapScreenState();
}
class MapScreenState extends State<ProfilePage> {
var users = new List<Users>();
MapScreenState() {
// Recupera os dados via json
_getDataProfile();
}
_getDataProfile() async {
var token = "ASDSDSADCSADASDSADASD";
final ws = Webservice();
ws.startLoading(context);
ws.getPerfil(token).then((ret) {
String retorno = ret["data"].toString();
setState(() {
Iterable lista = json.decode(json.encode(ret["data"]));
users = lista.map((model) => Users.fromJson(model)).toList();
});
});
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
itemBuilder: (BuildContext context, int index) {
log(users[index].user_name.toString());
}
},
),
}
}
How to solve the users[index]
? 'Cause he doesn’t exist until he finishes loading the http.post