Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FormatException: Invalid radix-10 number

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!!

like image 251
DanMossa Avatar asked Dec 28 '18 09:12

DanMossa


2 Answers

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

like image 134
shadowsheep Avatar answered Nov 20 '22 15:11

shadowsheep


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}'});
like image 28
Sardorek Aminjonov Avatar answered Nov 20 '22 17:11

Sardorek Aminjonov