【问题标题】:How to fix strange dart error on http call如何在 http 调用中修复奇怪的飞镖错误
【发布时间】:2019-08-23 02:49:34
【问题描述】:

我在运行时收到以下错误我正在执行 http 调用并返回 json。

"_TypeError (类型'List'不是类型'() => void'的子类型)"

这是我的代码

   class _ForumPostsState extends State<ForumPosts> {
List data;
String categoryID = 'D64D0737-746D-4562-8C10-6445F4069A92';
Future<String> getPostsByCategory() async {
    var response = await http.post(
 Uri.encodeFull("http://api/ForumPostsByCategory"),
      headers: {"Content-Type": "application/json", 
             'Accept': 'application/json',},
      body: json.encode({'categoryID' : categoryID }));  
     this.setState(
         data = json.decode(response.body)

     );
    print(data[1]["title"]);

return "Success!";
}

在这一行抛出错误

data = json.decode(response.body)

在调试时我注意到 JSON 在那里,它只是 data = json.decode 调用上的错误。

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    改变这个:

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

    到这里:

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

    更多信息在这里:https://docs.flutter.io/flutter/widgets/State/setState.html

    【讨论】:

    • 我不确定有什么区别,但谢谢你工作得很好
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 2020-02-22
    • 2023-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多