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

Tom*_*Tom 7 unhandled-exception dart flutter

我正在尝试从互联网上获取一些数据。所以我为一个天气网站做了一个 API 请求。但我收到以下异常 - 未处理的异常:FormatException: Invalid radix-10 number (at character 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<…>
Run Code Online (Sandbox Code Playgroud)

这是与之相关的代码:

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();
  }
}
Run Code Online (Sandbox Code Playgroud)

Ket*_*ani 37

`就我而言,我尝试使用以下方法解析 double [ amount = 12.34 ]

int.parse(amount)
Run Code Online (Sandbox Code Playgroud)

所以,改变为双倍对我有用`

double.parse(amount) 
Run Code Online (Sandbox Code Playgroud)


Bak*_*ker 8

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

例子:

print(int.parse('//ten'));
Run Code Online (Sandbox Code Playgroud)

会扔

print(int.parse('10'));
Run Code Online (Sandbox Code Playgroud)

应该管用。

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


Âng*_*tto 6

我遇到了这个问题,我解决了https://从 URL 地址中删除的问题。

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