Reputation: 1387
My function defination is below
Future<List<Item>> fetchGitUsers() async {
final response = await http.get('https://');
if (response.statusCode == 200) {
GitUsers gitUser = GitUsers.fromJson(json.decode(response.body));
return gitUser.items;
}
} / Function end
class GitUsers {
List<Item> items;
}
class ... extends State<SearchController> {
@override
void initState() {
super.initState();
gitUsers = fetchGitUsers() as List<Item>;
}
}
But I am getting below error on emulator screen..
Upvotes: 0
Views: 365
Reputation: 12673
You didn't add await
Try this
void getUsers() async{
gitUsers = await fetchGitUsers();
}
@override
void initState() {
super.initState();
getUsers();
}
}
If you want to use the git users in a UI (e.g ListView), consider using FutureBuilder.
Like this
FutureBuilder(
future: fetchGitUsers(),
builder: (context, snapshot){
if(!snapshot.hasData) return CircularProgressIndicator();
return ListView();
}
)
Upvotes: 1