Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
颤振http.get未执行_Http_Flutter_Dart - Fatal编程技术网

颤振http.get未执行

颤振http.get未执行,http,flutter,dart,Http,Flutter,Dart,我对flatter非常陌生,并遇到以下问题: child:RaisedButton( 已按下:(){ fetchData(); }, // ... fetchData()异步{ final res=等待http.get(“https://jsonplaceholder.typicode.com/posts"); 如果(res.statusCode==200){ //如果对服务器的调用成功,则解析JSON 打印(“它起作用”); 返回json.decode(res.body); }否则{ //如果

我对
flatter
非常陌生,并遇到以下问题:

child:RaisedButton(
已按下:(){
fetchData();
},
// ...
fetchData()异步{
final res=等待http.get(“https://jsonplaceholder.typicode.com/posts");
如果(res.statusCode==200){
//如果对服务器的调用成功,则解析JSON
打印(“它起作用”);
返回json.decode(res.body);
}否则{
//如果该调用未成功,则抛出一个错误。
抛出异常(“加载post失败”);
}
}
// ...

当我删除http.get部分时,它会打印“它工作了”,因此我认为问题在于http.get执行。

使按下异步,并添加等待获取数据().

使按下的异步并在获取数据()中添加一个等待

使您的方法类型为
未来
,并让它像这样返回数据类型
动态
。然后在任何需要的地方调用它

  Future<dynamic> fetchData() async {
    final res = await http.get("https://jsonplaceholder.typicode.com/posts");
    if (res.statusCode == 200) {
      // If the call to the server was successful, parse the JSON
      print("it works");
      return json.decode(res.body);
    } else {
      // If that call was not successful, throw an error.
      throw Exception('Failed to load post');
    }
  }
如果不想返回任何数据,只需调用该方法即可

  fetchData() async {
    final res = await http.get("https://jsonplaceholder.typicode.com/posts");
    if (res.statusCode == 200) {
      // If the call to the server was successful, parse the JSON
      print("it works");
      // do not return
      print(json.decode(res.body).toString());
    } else {
      // If that call was not successful, throw an error.
      throw Exception('Failed to load post');
    }
  }
只需像代码中的方法调用一样调用

    child: RaisedButton(
      // no need of anything now
      onPressed: () => fetchData()
     )

将您的方法类型设置为Future
,并让它像这样返回数据类型
dynamic
。然后在任何需要的地方调用它

  Future<dynamic> fetchData() async {
    final res = await http.get("https://jsonplaceholder.typicode.com/posts");
    if (res.statusCode == 200) {
      // If the call to the server was successful, parse the JSON
      print("it works");
      return json.decode(res.body);
    } else {
      // If that call was not successful, throw an error.
      throw Exception('Failed to load post');
    }
  }
如果不想返回任何数据,只需调用该方法即可

  fetchData() async {
    final res = await http.get("https://jsonplaceholder.typicode.com/posts");
    if (res.statusCode == 200) {
      // If the call to the server was successful, parse the JSON
      print("it works");
      // do not return
      print(json.decode(res.body).toString());
    } else {
      // If that call was not successful, throw an error.
      throw Exception('Failed to load post');
    }
  }
只需像代码中的方法调用一样调用

    child: RaisedButton(
      // no need of anything now
      onPressed: () => fetchData()
     )

通过运行flatter clean解决的问题仍然不知道如何解决

通过运行flatter clean解决的问题仍然不知道如何解决

需要更多的解释,它是否抛出错误
加载帖子失败
?不,这不是什么都没有发生尝试curl或postman为同一url发布结果帖子标题详细信息也需要更多的解释,是不是行错误
无法加载post
?不,这不是什么都没有发生尝试curl或postman对于同一url,也发布postman的resultPost头详细信息