输入的意外结束(在字符2处)
``
我正在尝试使用getx状态管理从API获取数据
import 'dart:convert';
import 'package:e_sante/Data/User.dart';
import 'package:get/get.dart';
import 'package:http/http.dart' as http;
class Controller extends GetxController{
var patientList=<Patient>[].obs;
var isLoading = true.obs;
@override
void onInit(){
super.onInit();
fetchPatientData();
}
Future<void> fetchPatientData() async{
final response= await http.get(Uri.parse('http://10.0.2.2:3000/patients?Ip=C123456'));
if(response.statusCode==200){
for(var i = 0; i < response.body.length; i++){
Patient patient= Patient.fromJson(jsonDecode(response.body[i]));
patientList.add(Patient(
Ip: patient.Ip,
Nom: patient.Nom,
Age: patient.Age,
Mail: patient.Mail,
Tel: patient.Tel,
Password: patient.Password),
);
}
isLoading.value=true;
}else{
Get.snackbar('Error loading data!', 'Sever responded: ${response.statusCode}:${response.reasonPhrase.toString()}');
}
}
}
class Patient{
final String Ip,Nom,Mail,Password;
final int Age,Tel;
Patient({
required this.Ip,
required this.Nom,
required this.Age,
required this.Mail,
required this.Tel,
required this.Password
});
factory Patient.fromJson(Map<String, dynamic> json){
return Patient(
Ip: json['Ip'],
Nom: json['Nom'],
Age: json['Age'],
Mail: json['Mail'],
Tel: json['Tel'],
Password: json['Password']
);
}
}
import 'package:get/get.dart';
import 'Data/controller.dart';
class ControllerBindings extends Bindings{ @override void dependencies() { Get.put<Controller>(Controller()); } }
时,当我热加载我的应用程序时,我遇到了此错误(未经措辞的例外:类型'list&lt; dynamic&gt;'不是类型的子类型'map&lt; string,dynamic&gt ;')并且当我打开屏幕时应显示数据时,屏幕继续加载
`
`
I'm trying to get the data from api using getx state management
import 'dart:convert';
import 'package:e_sante/Data/User.dart';
import 'package:get/get.dart';
import 'package:http/http.dart' as http;
class Controller extends GetxController{
var patientList=<Patient>[].obs;
var isLoading = true.obs;
@override
void onInit(){
super.onInit();
fetchPatientData();
}
Future<void> fetchPatientData() async{
final response= await http.get(Uri.parse('http://10.0.2.2:3000/patients?Ip=C123456'));
if(response.statusCode==200){
for(var i = 0; i < response.body.length; i++){
Patient patient= Patient.fromJson(jsonDecode(response.body[i]));
patientList.add(Patient(
Ip: patient.Ip,
Nom: patient.Nom,
Age: patient.Age,
Mail: patient.Mail,
Tel: patient.Tel,
Password: patient.Password),
);
}
isLoading.value=true;
}else{
Get.snackbar('Error loading data!', 'Sever responded: ${response.statusCode}:${response.reasonPhrase.toString()}');
}
}
}
class Patient{
final String Ip,Nom,Mail,Password;
final int Age,Tel;
Patient({
required this.Ip,
required this.Nom,
required this.Age,
required this.Mail,
required this.Tel,
required this.Password
});
factory Patient.fromJson(Map<String, dynamic> json){
return Patient(
Ip: json['Ip'],
Nom: json['Nom'],
Age: json['Age'],
Mail: json['Mail'],
Tel: json['Tel'],
Password: json['Password']
);
}
}
import 'package:get/get.dart';
import 'Data/controller.dart';
class ControllerBindings extends Bindings{ @override void dependencies() { Get.put<Controller>(Controller()); } }
when I'm hot reloading my app I encountered this error ( Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>') and when I open the screen who should display the data the screen keeps loading
`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您的错误消息,看起来像是JSON对象的API返回列表,但您正在尝试将JSON对象的此列表映射到JSON对象。尝试将响应映射到JSON列表,然后可能会起作用。
然后使用此患者映射API响应
According to your error message, it looks like your api return list of json object but you are try to map this list of json object to an json object. Try to map the response to list of json then may be it will work.
Then use this patientsFromJson to map the api response
1.算法误差分辨率
响应。Body是列表,但您可以添加到地图中,因此您可以更改此
2.当发生错误时,请注意将其停止加载
您必须捕获错误并停止加载,但是发生错误将来无法捕获。
因此,请尝试此
请参阅 https:// - 接受
1.fundamental error resolution
The response.body is List but you add into Map, so you can change this
2.expect it to stop the loading when If an error occurs
You have to catch error and stop loading but the error occurs can't catch in Future.
so try this
refer to https://dart.dev/guides/libraries/futures-error-handling