我如何才能在抖动中获得http响应

时间:2019-11-20 11:24:14

标签: flutter dart

当我导入课程时

import 'package:http/http.dart' as http;

按照https://flutter.dev/docs/cookbook/networking/fetch-data

它给出错误目标URL不存在

2 个答案:

答案 0 :(得分:0)

  1. 添加http包
import 'package:http/http.dart' as http;
  1. 发出网络请求
Future<http.Response> fetchPost() {
  return http.get('https://jsonplaceholder.typicode.com/posts/1');
}

在上面的示例中,目标网址是

  

https://jsonplaceholder.typicode.com/posts/1

相应的响应是

{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}

更多详细信息可用

https://flutter.dev/docs/cookbook/networking/fetch-data

答案 1 :(得分:0)

您必须将Http包添加到pubspec.yaml文件中的依赖项中,然后调用flutter包get

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: 0.1.2
  http: 0.12.0+2