如何在http调用上修复奇怪的dart错误

问题描述 投票:0回答:1

我在运行时收到以下错误我正在进行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个回答
1
投票

改变这个:

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

  );

对此:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.