12

I'm trying to receive a get request using Flutter and HttpClient.

This is complete code in what I'm trying to accomplish.

  getSuggest() async {
    try {
      var httpClient = new HttpClient();
      var uri = new Uri.http(
          'http://autocomplete.geocoder.api.here.com', '/6.2/suggest.json', {
        'app_id': 'APP_ID',
        'app_code': 'APP_CODE',
        'query': '123 Main Street',
        'country': 'USA',
        'language': 'en',
      });
      var request = await httpClient.getUrl(uri);
      var response = await request.close();
      var responseBody = await response.transform(Utf8Decoder()).join();
      Map data = jsonDecode(responseBody);
      print(data);
    } catch (error) {
      print(error);
    }
  }

And I'm using

import 'dart:io';
import 'dart:convert';

But my code always gets sent to the print(error) and the error that gets printed is

FormatException: Invalid radix-10 number

Any ideas?

Thanks!!

2
  • Can you post the whole error log ? Dec 28, 2018 at 9:53
  • Print response.body and post the output Dec 28, 2018 at 9:58

2 Answers 2

27

The problem is the scheme. You don't have to set it in Uri.http or Uri.https methods, it is set automatically, so change with the following:

Uri.http(
        'autocomplete.geocoder.api.here.com', '/6.2/suggest.json', {
        'app_id': 'APP_ID',
        'app_code': 'APP_CODE',
        'query': '123 Main Street',
        'country': 'USA',
        'language': 'en',
      });

And I suggest using http package and do something like that:

import 'package:http/http.dart' as http;
import 'dart:convert';


final json = const JsonCodec();
getSuggest() async {
    try {
      var uri = Uri.http(
          'autocomplete.geocoder.api.here.com', '/6.2/suggest.json', {
        'app_id': 'APP_ID',
        'app_code': 'APP_CODE',
        'query': '123 Main Street',
        'country': 'USA',
        'language': 'en',
      });
      var response = await http.get(uri);
      var data = json.decode(response.body);
      print(data);
    } catch (error) {
      print(error);
    }
  }

and use its http client if you need to set much more things (e.g. User Agents).

1
  • 1
    This is perfect!
    – DanMossa
    Dec 30, 2018 at 9:32
4
var url = Uri.https('http://192.168.43.5:3000/api', '/auth/getAllUsers', {'q': '{http}'});

The reason of this error is you do not have to provide https:// like above. Base url is enough. Additionally, you should use Uri.http instead of Uri.https, since your URL uses http:// (not https://). You should do it like the following.

var url = Uri.http('192.168.43.5:3000/api', '/auth/getAllUsers', {'q': '{http}'});

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.