flutter URI.https:类型“int”不是类型“Iterable<dynamic>”错误的子类型

say*_*bir 8 https flutter

我正在尝试调用 Web 服务上的方法作为 get http 方法。我收到错误,我不知道为什么会出错。这是我的方法:

  Future<dynamic> httpGet2({
    DirectsBody queryParams,
    HttpHeaderType headerType = HttpHeaderType.authenticated,
  }) async {
    Client clients = InterceptedClient.build(interceptors: [
      WeatherApiInterceptor(),
    ]);
    try {
      return _responseData(
        await clients.get(
          Uri.https(
            serverUrl,
            '/$apiKeyword/$path/',
            queryParams == null ? null : {'maxId': 0, 'minId': 0, 'count': 20},
          ),
          headers: HttpHeader.setHeader(headerType),
        ),
      );
    } on SocketException catch (e) {
      throw SocketException(e.url, e.key, e.value);
    }
  }
Run Code Online (Sandbox Code Playgroud)

我正在使用http_interceptor插件来提供帮助,但控制台上没有打印任何内容:

class WeatherApiInterceptor implements InterceptorContract {
  @override
  Future<RequestData> interceptRequest({RequestData data}) async {
    print('-------->  $data');
    return data;
  }

  @override
  Future<ResponseData> interceptResponse({ResponseData data}) async {
    print('-------->  $data');

    return data;
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我的错误:

E/flutter ( 4715): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'int' is not a subtype of type 'Iterable<dynamic>'
E/flutter ( 4715): #0      _Uri._makeQuery.<anonymous closure> (dart:core/uri.dart:2167:18)
E/flutter ( 4715): #1      _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:400:8)
E/flutter ( 4715): #2      _Uri._makeQuery (dart:core/uri.dart:2163:21)
E/flutter ( 4715): #3      new _Uri (dart:core/uri.dart:1427:13)
E/flutter ( 4715): #4      _Uri._makeHttpUri (dart:core/uri.dart:1597:12)
E/flutter ( 4715): #5      new _Uri.https (dart:core/uri.dart:1462:12)
Run Code Online (Sandbox Code Playgroud)

Uri.https不需要 Iterable,它只需要一个 Map !!!!所以我只是传递{'maxId': 0, 'minId': 0, 'count': 20},这是一个 Map 。

我尝试使用邮递员,一切都很好:

在此输入图像描述

Sah*_*ane 19

Uri.https(...)值中需要可迭代(对于查询参数),这String也意味着 Map 的值中的 a 。所以你需要将它从 转换Map<String, dynamic>Map<String, String>

因此,只需将Uri.https(..)代码中的部分更改为:

Uri.https(
    serverUrl,
    '/$apiKeyword/$path/',
    queryParams == null
        ? null
        : {'maxId': 0, 'minId': 0, 'count': 20}
            .map((key, value) => MapEntry(key, value.toString())),
  );
Run Code Online (Sandbox Code Playgroud)

因此,这里要删除的有效代码是:{...}.map((key, value) => MapEntry(key, value.toString()))