Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart Unhandled Exception: FormatException: Invalid radix-10 number (at character 1)

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();
  }
}
like image 654
Tom Avatar asked Mar 14 '21 00:03

Tom


5 Answers

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

like image 195
Ângelo Polotto Avatar answered Oct 09 '22 08:10

Ângelo Polotto


`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) 
like image 39
Ketan Ramani Avatar answered Oct 09 '22 06:10

Ketan Ramani


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.

like image 8
Baker Avatar answered Oct 09 '22 06:10

Baker


Use this code:

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

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

Solved the issue to me

like image 2
M. Massula Avatar answered Oct 09 '22 08:10

M. Massula


int.parse(amount);

Change it to

int.parse(amount != null ? amount : '0');
like image 1
Amoo Faruk Avatar answered Oct 09 '22 07:10

Amoo Faruk