我正试着从互联网上获取一些数据。所以我提出了一个天气网站的API请求。但我得到了以下异常-未处理的异常: FormatException:无效的基数-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();
}
}发布于 2021-05-14 23:57:27
我遇到了这个问题,我解决了从网址中删除https://的问题。
如果出于某种原因需要将https://保留在字符串中,请将Uri.https替换为Uri.parse
你可以在这里看到一个完整的例子:https://flutter.dev/docs/cookbook/networking/fetch-data
发布于 2021-03-14 11:01:15
该错误是Dart试图将字符串转换(解析)为整数,但该字符串不是有效的基数10 (?)数。
示例:
print(int.parse('//ten'));将抛出
print(int.parse('10'));应该行得通。
我猜Uri.https正在尝试int.parse(),以防您提供的地址是IP/IPv6地址(?)以及它被不正确的提供格式卡住了,正如jamesdin在评论中提到的那样。
发布于 2021-06-26 10:04:53
使用以下代码:
...
final uri = Uri.parse('your url here');
http.Response response = await http.get(uri);
...给我解决了这个问题
https://stackoverflow.com/questions/66619895
复制相似问题