【问题标题】:API work but return Instance of class nameAPI 工作但返回类名的实例
【发布时间】:2020-05-31 13:19:26
【问题描述】:

我有这个 API, 打印项目 oneJalsah 是正确的,但是当我返回 List 时出现问题,我进入了控制台 ALL = [“JalsahTime”实例、“JalsahTime”实例、“JalsahTime”实例、“JalsahTime”实例、“JalsahTime”实例、“JalsahTime”实例、“JalsahTime”实例、“JalsahTime”实例, “JalsahTime”实例, “JalsahTime”实例, “JalsahTime”实例, “JalsahTime”实例, “JalsahTime”实例, “JalsahTime”实例, “JalsahTime”实例, “JalsahTime”实例, 实例'JalsahTime' 的实例,'JalsahTime' 的实例,'JalsahTime' 的实例,'JalsahTime' 的实例,'JalsahTime' 的实例,'JalsahTime' 的实例,'JalsahTime' 的实例,'JalsahTime' 的实例,'JalsahTime' 的实例JalsahTime','JalsahTime' 实例,'JalsahTime' 实例,'JalsahTime' 实例]

这是我的鳕鱼

class Day1API{
  Future<List<JalsahTime>> getDay1()async{

    String url = "http://www.something.......";
    JalsahTime oneJalsah;
    List<JalsahTime> allJalsay =  List<JalsahTime>();
        print(url);
    var response = await http.get(url);
    if (response.statusCode == 200){
      var data = jsonDecode(response.body);
      print(data);
      for (var item in data){
        print("item = $item");
        oneJalsah = JalsahTime(item['id'], item['day'], item['start'], item['end'], item['date'], item['stateDay'], item['stateTime']);
        allJalsay.add(oneJalsah);
      }
      print("ALL = $allJalsay");
      print('jalsah done');
      return allJalsay;
    }
    else print(" day failed");
  }
}

这是我的课

class JalsahTime{
  String id,day,start,end,date,stateDay,stateTime;
  JalsahTime(this.id, this.day, this.start, this.end, this.date, this.stateDay,
      this.stateTime);
}

【问题讨论】:

  • 实现JalsahTime.toString()方法
  • 但不是字符串,是类列表
  • 你有JalsahTime 类,对吧?我假设它是你的 pwn 类(不是来自任何包),所以在该类中添加 toString 方法
  • 我编辑问题以添加我的班级
  • 你所要做的就是在你的类中添加toString()方法

标签: list api flutter dart instance


【解决方案1】:

toString 方法添加到您的类中

class JalsahTime{
  String id,day,start,end,date,stateDay,stateTime;
  JalsahTime(this.id, this.day, this.start, this.end, this.date, this.stateDay,
      this.stateTime);

  @override
  String toString(){
      return "Some representation of that instance: ${this.id}"   
  }
}

【讨论】:

  • 其他变量呢? ,day,start,end,date,stateDay,stateTime;?
  • 您需要将它们添加到字符串中。
猜你喜欢
  • 2017-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多