1

我正在尝试使用 Dart 的http库来发出简单的 HTTP GET 和 POST 请求:

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

void main() {
    String json = getSomeJSONString();
    http.post(url, body: json, encoding: Encoding.getByName("UTF-8")).then(onResponse);
}

void onResponse(Response response) {
    // Do something
}

当我运行它时,pub build我得到:

[Dart2JS on myapp-client|web/myapp_client.dart]:Building myapp-client......

[Dart2JS on myapp-client|web/myapp_client.dart]:
../../../../../sandbox/workbench/dart/dart/dart-sdk/lib/core/uri.dart:1133:17: Info: This is the method declaration.packages/http/src/utils.dart:41:42: Warning: Arguments do not match the expected parameters of 'encodeQueryComponent'.
static String encodeQueryComponent(String component) {
^^^^^^^^^^^^^^^^^^^^

[Dart2JS on myapp-client|web/myapp_client.dart]:pairs.add([Uri.encodeQueryComponent(key, encoding: encoding),

../../../../../sandbox/workbench/dart/dart/dart-sdk/lib/core/uri.dart:1133:17: Info: This is the method declaration.
static String encodeQueryComponent(String component) {
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

[Dart2JS on myapp-client|web/myapp_client.dart]:
packages/http/src/utils.dart:42:42: Warning: Arguments do not match the expected parameters of 'encodeQueryComponent'.
 Uri.encodeQueryComponent(value, encoding: encoding)]));
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
[Dart2JS on myapp-client|web/myapp_client.dart]:
packages/http/src/utils.dart:41:58: Warning: No named argument 'encoding' found on method.
pairs.add([Uri.encodeQueryComponent(key, encoding: encoding),
 ^^^^^^^^
[Dart2JS on myapp-client|web/myapp_client.dart]:
packages/http/src/utils.dart:42:60: Warning: No named argument 'encoding' found on method.
 Uri.encodeQueryComponent(value, encoding: encoding)]));
 ^^^^^^^^
[Info in Dart2JS]:
Generated myapp-client|web/myapp_client.dart.js (246641 characters) in 0:00:09.640230
Built 14 files!

所以有几个问题:

  1. 这些警告是什么,为什么我会收到它们?
  2. 我需要做什么才能让他们离开(修复他们)?
  3. http如果它是 Dart 语言/核心的一部分,为什么我需要将此库作为依赖项包含在内?
4

1 回答 1

2

The http package depends on dart:io, which is not available to Web client applications, so you will not be able to use it (this is not the source of your warnings, but there is not much point debugging them when you can't use the package anyway).

Use something like adaj instead.

As far as I understand the http package is not part of the Dart core libraries, though it is worked on by the Dart team. I am not entirely sure what the decision is for making a library part of Dart core or not (dart: prefix). In this case I assume it is because dart:io already has http support.

于 2013-12-26T18:56:28.157 回答