输入的意外结束(在字符2处)

发布于 2025-01-18 18:24:41 字数 2040 浏览 0 评论 0原文

``

我正在尝试使用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 loadingError screen

`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流星番茄 2025-01-25 18:24:41

根据您的错误消息,看起来像是JSON对象的API返回列表,但您正在尝试将JSON对象的此列表映射到JSON对象。尝试将响应映射到JSON列表,然后可能会起作用。

List<Patient> patientsFromJson(String body) =>
    List<Patient>.from(jsonDecode(body).map((x) => Patient.fromJson(x)));

然后使用此患者映射API响应

Future<void> fetchPatientData() async{
    final response= await http.get(Uri.parse('http://10.0.2.2:3000/patients?Ip=C123456'));

    if(response.statusCode==200){
      var patients = patientsFromJson(response.body);

      for (var patient in patients) {
        patientList.add(patient);
      }

     // patientList.addAll(patients);
      isLoading.value=true;
    }else{
      Get.snackbar('Error loading data!', 'Sever responded: ${response.statusCode}:${response.reasonPhrase.toString()}');
    }
  }

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.

List<Patient> patientsFromJson(String body) =>
    List<Patient>.from(jsonDecode(body).map((x) => Patient.fromJson(x)));

Then use this patientsFromJson to map the api response

Future<void> fetchPatientData() async{
    final response= await http.get(Uri.parse('http://10.0.2.2:3000/patients?Ip=C123456'));

    if(response.statusCode==200){
      var patients = patientsFromJson(response.body);

      for (var patient in patients) {
        patientList.add(patient);
      }

     // patientList.addAll(patients);
      isLoading.value=true;
    }else{
      Get.snackbar('Error loading data!', 'Sever responded: ${response.statusCode}:${response.reasonPhrase.toString()}');
    }
  }
如梦亦如幻 2025-01-25 18:24:41

1.算法误差分辨率
响应。Body是列表,但您可以添加到地图中,因此您可以更改此

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),
      );

}

2.当发生错误时,请注意将其停止加载
您必须捕获错误并停止加载,但是发生错误将来无法捕获。
因此,请尝试此

//somewhere 
fetchPatientData().then(processValue).catchError(handleError);

请参阅 https:// - 接受

1.fundamental error resolution
The response.body is List but you add into Map, so you can change this

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),
      );

}

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

//somewhere 
fetchPatientData().then(processValue).catchError(handleError);

refer to https://dart.dev/guides/libraries/futures-error-handling

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文