RangeError(索引):无效值:有效值范围为空:Flutter中为0

时间:2020-06-04 08:40:22

标签: flutter

我正在从API提取数据到列表中,此错误“ RangeError(index):无效值:有效值范围为空:0”出现几秒钟,然后消失,数据正确显示。 这是我正在获取数据的地方:

  _RendezVousMedecinState() {
    /* Fetching Data Into ListView */

    Future<String> getData() async {
      var response = await http.get(
          Uri.encodeFull("http://192.168.1.6:4000/user/GetRdvMedecin"),
          headers: {
            "Accept": "application/json",
            "token" : "5e5aa4ad190d8c2818a66a08"
          }
      );

      this.setState(() {

        this.data = json.decode(response.body);
      });


      return "Success";
    }

//Get Patient Data
    Future<String> getPatientData(String idpatient) async {
      var response = await http.get(
          Uri.encodeFull("http://192.168.1.6:4000/user/GetUserByID"),
          headers: {
            "Accept": "application/json",
            "token" : idpatient
          }
      );
      this.setState(() {


        this.Patients = json.decode(response.body);
      });


      return "Success";
    }
//Get Patient Data

    getData().then((data) async {
      for(int i=0 ; i<this.data.length;i++){
             if(this.data[i]['idpatient'] != null ){
               getPatientData(this.data?.elementAt(i)["idpatient"]).then((data) async {
                 for(int j=0 ; j<this.data.length;j++){
                   Patientsitems.add(new Patient(this.Patients?.elementAt(j)['nom'],this.Patients?.elementAt(j)['prenom']));
                 }
               });
               items.add(new RendezVous("assets/images/patient.png", this.data?.elementAt(i)["idpatient"], this.data?.elementAt(i)["idmedecin"],this.data?.elementAt(i)["_id"]));

             }
      }
    });



    /* Fetching Data Into ListView */
  }

,这里是我在列表视图中显示数据的代码:

                this.items.length > 0      ?   ListView.builder(
                itemCount: items.length,
                itemBuilder: (context, index) => MedcCell(context, index),
              )

请问如何解决此错误?

0 个答案:

没有答案