我使用 dio 和 getx 来获取待办事项数据列表,但我看到了这条消息
type 'List<dynamic>' is not a subtype of type 'List<Todo>'
当我到达这条线时
列出 responseBode = rsp.data['data'];
这行发生了什么以及如何解决它,谢谢
服务类更新为转换为列表,但仍然是同样的问题
1- 控制器的第一个类 2- 服务类的第二个
class TodoListController extends GetxController {
//var lstTask = List<dynamic>.empty(growable: true).obs;
var items = List<Todo>.empty(growable: true).obs;
var page = 1.obs;
var totalRows = 10.obs;
var isDataProcessing = false.obs;
var isMoreDataAvailable = true.obs;
var isSearching = false.obs;
var isLoading = false.obs;
var isCompleted = true.obs;
var errorMsg = ''.obs ;
var http = new HttpUtils();
// For Pagination
ScrollController scrollController = ScrollController();
@override
onInit() {
super.onInit();
// Fetch Data
getTodo(page);
}
// Fetch Data
getTodo(var page) {
try {
isMoreDataAvailable(false);
isDataProcessing(true);
AppServices().getTodo(page).then((resp) {
isDataProcessing(false);
resp.forEach(( dynamic json) {
items.add(Todo.fromJson(json));
});
print(resp);
//items.addAll(resp);
}, onError: (err) {
isDataProcessing(false);
AppServices().showSnackBar("Error", err.toString(), Colors.red);
});
} catch (exception) {
isDataProcessing(false);
AppServices().showSnackBar("Exception", exception.toString(), Colors.red);
}
}
// Get More data
getMoreTodo(var page) {
try {
AppServices().getTodo(page).then((resp) {
if (resp.length > 0) {
isMoreDataAvailable(true);
} else {
isMoreDataAvailable(false);
AppServices().showSnackBar("Message", "No more items", Colors.lightBlueAccent);
}
items.addAll(resp);
}, onError: (err) {
isMoreDataAvailable(false);
AppServices().showSnackBar("Error", err.toString(), Colors.red);
});
} catch (exception) {
isMoreDataAvailable(false);
AppServices().showSnackBar("Exception", exception.toString(), Colors.red);
}
}
}
类 AppServices 扩展 GetConnect { var http = new HttpUtils();
// Fetch Data
Future<List<Todo>> getTodo(var id) async {
try {
Response rsp = await http.get(AppUrl.get_todo, {'current_page': id});
print(rsp.data['data']);
if (rsp.data["response_status"] != '400') {
List<Todo> responseBode = rsp.data['data'].map ((todo) => Todo.fromJson(todo)).toList();
return responseBode;
} else {
return rsp.data["response_message"] ;
}
}catch(exception)
{
return Future.error(exception.toString());
}
}
}