Home > database >  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>'

Time:10-01

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

CodePudding user response:

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()))

  • Related