Can someone help me to fix this problem. i can't fetch data from json using flutter http request because it got this Unhandled Exception: type 'List' is not a subtype of type 'String' and want to load more data when scroll to bottom of screen. Can somebody help me please...
postData.dart file
class PostData extends StatefulWidget {
const PostData({Key key}) : super(key: key);
@override
_PostDataState createState() => _PostDataState();
}
class _PostDataState extends State<PostData> {
List<String> getData = List();
ScrollController _scrollController = ScrollController();
@override
void initState() {
super.initState();
fetchFive();
_scrollController.addListener(() {
if (_scrollController.position.pixels ==
_scrollController.position.maxScrollExtent) {
fetchFive();
}
});
}
@override
void dispose() {
_scrollController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Load Page"),
toolbarHeight: 65,
backgroundColor: Colors.blueAccent[700],
),
body: ListView.builder(
controller: _scrollController,
itemCount: getData.length,
itemBuilder: (context, index) {
return Card(
child: GestureDetector(
child: ListTile(
title: Text(getData[index]),
),
),
);
}));
}
fetch() async {
final response =
await http.get('https://my-json-server.typicode.com/typicode/demo/posts');
if (response.statusCode == 200) {
setState(() {
getData.add(json.decode(response.body));
});
} else {
throw Exception('Failed to load data');
}
}
fetchFive() {
for (int i = 0; i < 5; i++) {
fetch();
}
}
}
getData.dart file model json
class Users {
final int no;
final String nosiri;
final String lokasi;
final String status;
final String pemilik;
final String thumbnailurl;
Users(
{this.no,
this.nosiri,
this.lokasi,
this.status,
this.pemilik,
this.thumbnailurl});
factory Users.fromJson(Map<String, dynamic> json) {
return Users(
no: json['no'],
nosiri: json['no_siri'],
lokasi: json['lokasi'],
status: json['status'],
pemilik: json['pemilik'],
thumbnailurl: json['thumbnailurl'],
);
}
}