Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

https

flutter

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

like image 867
Cyrus the Great Avatar asked May 11 '26 08:05

Cyrus the Great


1 Answers

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

like image 200
Sahil Sonawane Avatar answered May 13 '26 00:05

Sahil Sonawane



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!