Dart 未处理的异常:FormatException:无效的 radix-10 数字(在字符 1 处)

问题描述 投票:0回答:13

我正在尝试从互联网上获取一些数据。所以我为一个天气网站做了一个 API 请求。但我收到以下异常 - 未处理的异常:FormatException:无效的 radix-10 数字(在字符 1 处)。这是错误代码:

    [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<…>

这是随之而来的代码:

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();
  }
}
flutter dart unhandled-exception formatexception
13个回答
40
投票

我遇到了这个问题,我解决了只是从 URL 地址中删除

https://

如果您出于某种原因需要将

https://
保留在字符串中,请将
Uri.https
替换为
Uri.parse

你可以在这里看到一个完整的例子:https://flutter.dev/docs/cookbook/networking/fetch-data


32
投票

`在我的例子中,我尝试使用 double [ amount = 12.34 ] 解析

int.parse(amount)

所以,改成双人对我有用`

double.parse(amount) 

8
投票

该错误是 Dart 试图将字符串转换(解析)为整数,但字符串不是有效的以 10(?)为基数的数字。

例子:

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

会扔

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

应该工作。

我猜

Uri.https
正在尝试
int.parse()
以防万一您提供的地址是 IP/IPv6 地址(?)并且它因提供的格式不正确而窒息,正如 jamesdin 在评论中提到的那样。


3
投票

使用此代码:

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

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

解决了我的问题


1
投票
int.parse(amount);

改成

int.parse(amount != null ? amount : '0');

1
投票

Uri.http()
解析字符串的问题。我通过使用基本 Uri 构造函数解决了这个问题。

例如:

var uri = Uri(scheme:'http', host: '127.0.0.1', port: 8000, path: '/your-pass');

0
投票

我有一个类似的问题,但这是因为我在网址末尾添加了'/',如

192.168.178.28:8080/
,我只是使用了
192.168.178.28:8080

我正在使用这样的代码来获取用于开发或生产的适当 URL。

  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;
  }

0
投票

对于Flutter,你需要两个参数,第一个你需要进入域,第二个传递子路由

 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': '[email protected]', 'password': 'usersecretpwd'});
      print(response.body);
    } finally {
      client.close();
    }
  }

0
投票

您可以使用此代码检查数字是否小于 1

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

希望这会有所帮助:)。


0
投票

如果您发现自己在这里并且问题不是关于 uri 并且您正在尝试输入一个数字,只需将 int.parse 更改为 double.parse 。


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

0
投票

在我的案例中,问题发生在尝试使用格式货币解析 double 时。

因为输入键盘加了逗号(,)而不是点(.),double.parse()方法无法正确解析,所以在尝试解析之前必须把所有的(,)替换为(.)。

double.parse(controller.value.text.replaceAll(',', '.')

0
投票
  1. Uri.parse
    代替
    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. AndroidManifest.xml
    文件中提供互联网许可。
<uses-permission android:name="android.permission.INTERNET"/>
  1. 如果您没有登录游戏商店,请在模拟器中登录游戏商店。 (不是警告)
© www.soinside.com 2019 - 2024. All rights reserved.