47

I am trying to fetch some Data from the internet. So I made a API Request for a weather website. But I am getting the following exception- Unhandled Exception: FormatException: Invalid radix-10 number (at character 1). This is the error Code:

    [VERBOSE-2:shell.cc(242)] Dart Unhandled Exception: FormatException: Invalid radix-10 number (at character 1)
//uri.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=4659036c3236...
^
, stack trace: #0      int._throwFormatException (dart:core-patch/integers_patch.dart:131:5)
#1      int._parseRadix (dart:core-patch/integers_patch.dart:157:16)
#2      int._parse (dart:core-patch/integers_patch.dart:100:12)
#3      int.parse (dart:core-patch/integers_patch.dart:63:12)
#4      _Uri._makeHttpUri (dart:core/uri.dart:1591:49)
#5      new _Uri.https (dart:core/uri.dart:1462:12)
#6      _LoadingScreenState.getData (package:clima/screens/loading_screen.dart:25:49)
#7      _LoadingScreenState.build (package:clima/screens/loading_screen.dart:37:5)
#8      StatefulElement.build (package:flutter/src/widgets/framework.dart:4612:27)
#9      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4495:15)
#10     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4<…>

This is the code that goes with that:

import 'package:flutter/material.dart';
import 'package:clima/services/location.dart';
import 'package:http/http.dart' as http;

class LoadingScreen extends StatefulWidget {
  @override
  _LoadingScreenState createState() => _LoadingScreenState();
}

class _LoadingScreenState extends State<LoadingScreen> {
  @override
  void initState() {
    super.initState();
    getLocation();
  }

  void getLocation() async {
    Location location = Location();
    await location.getCurrentLocation();
    print(location.latitude);
    print(location.longitude);
  }

  void getData() async {
    http.Response response = await http.get(Uri.https(
        'https://uri.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=4659036c323608514eb865c174726965',
        'albums/1'));

    if (response.statusCode == 200) {
      String data = response.body;
      print(data);
    } else {}
  }

  @override
  Widget build(BuildContext context) {
    getData();
    return Scaffold();
  }
}
Tom
  • 503
  • 1
  • 4
  • 8
  • 4
    You are misusing `Uri.https`; its first argument should be a hostname, not a hostname with a path. You should be using [`Uri.parse`](https://api.dart.dev/stable/dart-core/Uri/parse.html) instead. – jamesdlin Mar 14 '21 at 00:46
  • You should also avoid calling methods inside the build method of a widget tree , because every time the widget gets rebuilt getData(); will be called. Call it in the initState – croxx5f Mar 14 '21 at 01:28

14 Answers14

42

I faced this problem and I solved just removing the https:// from the URL address.

If you need keep the https:// in your string for some reason, replace Uri.https to Uri.parse

You can see a complete example here: https://flutter.dev/docs/cookbook/networking/fetch-data

Ângelo Polotto
  • 8,463
  • 2
  • 36
  • 37
  • this comment gave me a hint, i wasnt using http:// or https:// but i was using a path at the end something like... example.compute.amazonaws.com/api/user.. solved it by renoving /api/users and just leaving `example.compute.amazonaws.com` – Shender Ramos Aug 06 '22 at 21:05
34

`In my case I'm trying to parse double [ amount = 12.34 ] using

int.parse(amount)

So, Changing to double works for me`

double.parse(amount) 
Ketan Ramani
  • 4,874
  • 37
  • 42
8

That error is Dart attempting to convert (parse) a String into an Integer, but the String isn't a valid base 10 (?) number.

Example:

print(int.parse('//ten'));

will throw

print(int.parse('10'));

should work.

I guess that Uri.https is trying an int.parse() in case the address you've supplied is an IP/IPv6 address (?) and its choking on the incorrect supplied format, as mentioned by jamesdin in the comments.

Baker
  • 24,730
  • 11
  • 100
  • 106
3

Use this code:

...
final uri = Uri.parse('your url here');

http.Response response = await http.get(uri);
...

Solved the issue to me

M. Massula
  • 3,730
  • 2
  • 11
  • 14
2
int.parse(amount);

Change it to

int.parse(amount != null ? amount : '0');
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Amoo Faruk
  • 63
  • 5
2

It is issue of parsing string by Uri.http(). I resolved this by using base Uri constructor.

For example:

var uri = Uri(scheme:'http', host: '127.0.0.1', port: 8000, path: '/your-pass');
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
JapanVibe
  • 21
  • 1
0

I had a similar issue but this is because I added '/' at the end of the url like 192.168.178.28:8080/ and I just used 192.168.178.28:8080.

I am using a code like this to get the appropriate URL for Development or Production.

  static const String ENV = 'DEV'; // 'PROD';
  static const String API_URL_PROD = "myapi.herokuapp.com";
  static const String API_URL_DEV = "192.168.178.98:8080";

  Uri getUrlForEnv(String endpoint) {

     var url;

     if (Constant.ENV == 'PROD') {
        url = Uri.https(Constant.API_URL_PROD, endpoint);
     } else {
        url = Uri.http(Constant.API_URL_DEV, endpoint);
     }
     return url;
  }
biniam
  • 8,099
  • 9
  • 49
  • 58
0

for Flutter, you need two parameters, in first you need to enter domain, and in second pass the subroute

 void loginUser() async {
  //  Dialogs.showProgressDialog(context: context);
    var client = http.Client();
    try {
      var response = await client.post(
          Uri.https(ApiUrls.BASE_URL,"/user/loginUser"),
          body: {'email': 'pragna@domain.com', 'password': 'usersecretpwd'});
      print(response.body);
    } finally {
      client.close();
    }
  }
Developer
  • 333
  • 2
  • 16
0

You can check that a number is less than 1 with this code

value.numericOnly().characters.first == "0"

Hope this will be helpful :).

gtr Developer
  • 2,369
  • 16
  • 12
0

if you found yourself here and the problem wasn't about uri and you're trying to cast a number just change int.parse into double.parse .

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 10 '22 at 21:54
0
  1. Use Uri.parse instead of Uri.https
http.Response response = await http.get(
  Uri.parse(
    'https://uri.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=4659036c323608514eb865c174726965',
    'albums/1',
  ),
);
  1. Provide internet permission in AndroidManifest.xml file.
<uses-permission android:name="android.permission.INTERNET"/>
  1. If you don't sign in play store please sign in play store in emulator. (Not mendetory)
its-me-mahmud
  • 690
  • 1
  • 7
  • 14
Majedul Islam
  • 213
  • 2
  • 5
0
//Use this method to avoid radix -10 error in dart


String str = "Hafce00453dfg234fdksd";

//get the integer
int newnum1 = int.parse(str.replaceAll(RegExp(r'[^0-9]'),''));
print(newnum1); //output: 453234

//int newnum2 = int.parse(str);
//dont do this: Error: Invalid radix-10 number
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
0

The problem in my case happened when trying to parse a double using format currency.

Because the input keyboard adds comas (,) instead of dots (.), the method double.parse() can’t parse correctly, so before trying to parse you have to replace all the (,) for (.).

double.parse(controller.value.text.replaceAll(',', '.')
Jordi CRod
  • 111
  • 1
  • 3
0

for https://orangevalleycaa.org/api/videos and http: ^0.13.6

var url = Uri.https('www.orangevalleycaa.org', '/api/videos', {'q': '{http}'});
 var response = await http.get(url)
Moccine
  • 1,021
  • 6
  • 14