I am trying to fetch data from Api but I get following error:
Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List'
I copied the code from a Video with a different Api and there it works fine. So I guess its a Problem with the definied API (https://api.radioking.io/widget/radio/leipzig-beatz/track/current).
It looks like the error is happening on this line:
setState(() {
users = items;
isLoading = false;
});
This is the Code:
List users = [];
bool isLoading = false;
@override
void initState() {
// TODO: implement initState
super.initState();
this.fetchUser();
}
fetchUser() async {
setState(() {
isLoading = true;
});
var url =
"https://api.radioking.io/widget/radio/leipzig-beatz/track/current";
var response = await http.get(url);
// print(response.body);
if (response.statusCode == 200) {
var items = json.decode(response.body);
setState(() {
users = items;
isLoading = false;
});
} else {
users = [];
isLoading = false;
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Listing Users"),
),
body: getBody(),
);
}
Widget getBody() {
if (users.contains(null) || users.length < 0 || isLoading) {
return Center(child: CircularProgressIndicator());
}
return ListView.builder(
itemCount: users.length,
itemBuilder: (context, index) {
return getCard(users[index]);
});
}
Widget getCard(item) {
var title = item['title'];
return Card(
elevation: 1.5,
child: Padding(
padding: const EdgeInsets.all(10.0),
child: ListTile(
title: Row(
children: <Widget>[
SizedBox(
width: 20,
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(
width: MediaQuery.of(context).size.width - 140,
child: Text(
title,
style: TextStyle(fontSize: 17),
)),
SizedBox(
height: 10,
),
],
)
],
),
),
),
);
}