在这个UI中,当我运行应用程序时,前10个数据加载,但分页不起作用,所以它不会加载接下来的10个项目,但当我对UI进行一些更改并运行热重载时,它就可以正常工作了。API运行得很好,我不知道为什么UI会这样。我需要帮助,请看下面的代码。
感谢
UI
child: FutureBuilder(
future: getData('0'),
builder: (context, snapshot){
if (snapshot.connectionState == ConnectionState.done) {
if (contactModel.status == 'true'){
return ListView.builder(
controller: scrollController,
itemCount: contacts.length,
itemBuilder: (BuildContext context, int index) {
return listViewItem(contacts[index]);
});
}
else{
return Center();
}
}
else {
return Center(child: CircularProgressIndicator());
}
}
),
数据调用
Future<ContactModel> getData(String lastId) async {
empid = await SharedPreferencesClass().getempid();
tkc_id = await SharedPreferencesClass().gettkcid();
try {
Dio dio = new Dio();
dio.options.connectTimeout = 50000;
dio.options.contentType = "multipart/form-data";
FormData formData;
formData = FormData.fromMap({
"emp_id": '11409',
"tkc_id": 'TKC0011',
"selected_tkcid": 'TKC0011',
"last_id": lastId,
"contactval":'A',
"locality":'A',
"custtype":'A',
});
Response response = await dio.post(HttpUrl.getcrmcustomerlist, data: formData);
// print('Server response: ' + response.toString());
if (response.statusCode == 200) {
Map<String, dynamic> responseBody = response.data;
contactModel = ContactModel.fromJson(responseBody);
contacts.addAll(contactModel.contacts);
} else {
Fluttertoast.showToast(
msg: "Please Try After Some Time",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
);
}
} catch (e) {
print(e);
}
return new ContactModel();
}
InitState上的滚动控制器
void initState() {
super.initState();
scrollController.addListener(() {
if(scrollController.position.pixels >= scrollController.position.maxScrollExtent ){
// print('Data Called');
Future.delayed(Duration(milliseconds: 2000));
getData(contacts.last.customer_id);
}
});
}
我相信添加setState会解决这个问题,就像它不会加载任何数据
if (response.statusCode == 200) {
Map<String, dynamic> responseBody = response.data;
contactModel = ContactModel.fromJson(responseBody);
setState(() {
contacts.addAll(contactModel.contacts);
})
} else {
Fluttertoast.showToast(
msg: "Please Try After Some Time",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
);
我自己解决了这个问题是的,我猜是对的,这只是setState的问题。
在这里,我用setState包装了联系人,但后来我意识到我也应该包装contactModel。
if (response.statusCode == 200) {
Map<String, dynamic> responseBody = response.data;
contactModel = ContactModel.fromJson(responseBody);
setState(() {
contacts.addAll(contactModel.contacts);
})
像这样
if (response.statusCode == 200) {
Map<String, dynamic> responseBody = response.data;
setState(() {
contactModel = ContactModel.fromJson(responseBody);
contacts.addAll(contactModel.contacts);
});
}