【问题标题】:type 'String' is not a subtype of type 'int' of 'index' while fetching data from json file in flutter在flutter中从json文件中获取数据时,类型'String'不是'index'类型'int'的子类型
【发布时间】:2021-07-06 18:21:16
【问题描述】:

当我想从一个 json 表单中获取一个值时,我遇到了这个错误,上面写着 type 'String' is not a subtype of type 'int' of 'index' 什么是问题?我正在使用http: ^0.12.0+2

this is the output of the program

这是我使用的代码:

void getData() async{

  Response res=await get("https://jsonplaceholder.typicode.com/posts");
  String dat=res.body;
  var datta=jsonDecode(dat)['title'];

  print(datta);

  }

【问题讨论】:

  • 尝试转换为字符串。因此:value.toString();

标签: flutter dart


【解决方案1】:

在您的代码中,jsonDecode(dat)List,而不是 Map

List<Map<String, dynamic>> dataList = (jsonDecode(dat) as List).map((e)=>(e as Map)).toList();

【讨论】:

  • 它显示此错误“'List' 类型的值不能分配给 'List>' 类型的变量”
  • 我刚刚更新了我的答案。其实可以定义Class来转换数据。
猜你喜欢
  • 2021-12-15
  • 2022-11-25
  • 2020-04-22
  • 2020-10-02
  • 2021-12-19
  • 1970-01-01
  • 2021-05-10
  • 2019-04-24
  • 2021-03-29
相关资源
最近更新 更多