flutter URI.https: type 'int' is not a subtype of type 'Iterable<dynamic>' 错误

flutter URI.https: type 'int' is not a subtype of type 'Iterable<dynamic>' error

我正在尝试调用 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);
    }
  }

我正在使用 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;
  }
}

这是我的错误:

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)

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

我试了一下邮递员,一切都很好:

Uri.https(...) 需要在值(对于查询参数)中进行迭代,这意味着在 Map 的值中也有一个 String。所以你需要把它从 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())),
  );

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