7

I am trying to call a method on web service as a get http method. I got error that I don't know why I got error. This is my method:

  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);
    }
  }

I am using http_interceptor plugin to help but nothing is printed on console :

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;
  }
}

This is my error :

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 doesn't need Iterable, It just need a Map !!!!SO I just pass {'maxId': 0, 'minId': 0, 'count': 20}, that is a map .

I try on postman and everything was good :

enter image description here

Cyrus the Great
  • 5,145
  • 5
  • 68
  • 149
  • What do you mean of ` json.parse` ? json did not have parse method @Vayhuit – Cyrus the Great Sep 30 '21 at 07:59
  • 2
    It seems like either a bug in the implementation of `Uri.http`/`Uri.https` or a bug in the documentation. For now use `{'maxId': '0', 'minId': '0', 'count': '20'}`. – jamesdlin Sep 30 '21 at 08:05
  • 1
    https://github.com/dart-lang/sdk/issues/46251 – jamesdlin Sep 30 '21 at 08:06
  • Also, the relevant documentation for how the `queryParameters` argument is supposed to work is in the [*default* `Uri` constructor](https://api.dart.dev/stable/dart-core/Uri/Uri.html). Sigh. – jamesdlin Sep 30 '21 at 08:17
  • No place in doc I can't see `iterable` as queryParams, need a map @jamesdlin – Cyrus the Great Sep 30 '21 at 08:18
  • 1
    I meant that the documentation for the default `Uri` constructor explains that the `Map` values are required to be either `String`s or `Iterable`s. – jamesdlin Sep 30 '21 at 08:46

1 Answers1

14

Uri.https(...) needs iterable in the value (for query params) by which it means a String in the value of the Map too. So you need to convert it from Map<String, dynamic> to Map<String, String>

So just change Uri.https(..) part in your code to:

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

Therefore, effective code here to take away is: {...}.map((key, value) => MapEntry(key, value.toString()))

Sahil Sonawane
  • 1,144
  • 9
  • 18
  • 1
    `String`s aren't `Iterable`s. As explained by [the documentation for the default `Uri` constructor](https://api.dart.dev/stable/dart-core/Uri/Uri.html), the `Map` is expected to have values that either are `String`s or `Iterable`s. – jamesdlin Sep 30 '21 at 08:16
  • Yeah, you're right, what I meant by that was it needs `Iterable` where also `String` is `Iterable` you can iterate over. So either `String` or other iterable is something it requires! – Sahil Sonawane Sep 30 '21 at 08:21