【问题标题】:json.decode(response.body)..."Here response.body is not working..." [duplicate]json.decode(response.body)......“这里的response.body不起作用......” [重复]
【发布时间】:2021-03-13 04:00:31
【问题描述】:

请帮帮我... 我刚试过这段代码:

**try {
      final response = http.get(url);                       // Giving error here 
      final extractedData = json.decode(response.body);
    } catch (error) {
      throw error;
    }**

错误信息 "message": "没有为类型 'Future' 定义 getter 'body'。\n尝试导入定义 'body' 的库,将名称更正为现有 getter 的名称,或者定义一个名为'身体'。",

【问题讨论】:

    标签: json flutter http dart fetchrequest


    【解决方案1】:
    try {
          final response = await http.get(url);   
          final extractedData = json.decode(response.body);
        } catch (error) {
          throw error;
        }
    

    您收到此错误是因为 get() 返回一个 Future<Response>,这意味着它是异步的,因此您需要使用 async/await 才能获取 Response 对象,然后调用 body

    请检查以下内容:

    https://dart.dev/codelabs/async-await

    https://stackoverflow.com/a/748189/7015400

    【讨论】:

      猜你喜欢
      • 2020-04-11
      • 1970-01-01
      • 1970-01-01
      • 2018-11-05
      • 2021-03-04
      • 1970-01-01
      • 2021-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多