首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未处理的异常: FormatException:无效的基数-10数字(位于字符1)

未处理的异常: FormatException:无效的基数-10数字(位于字符1)
EN

Stack Overflow用户
提问于 2020-12-31 02:06:35
回答 2查看 6.9K关注 0票数 0

我正在跟随这个颤动初学者教程,从我在斯里兰卡的时区here .But 5:30开始,所以我试图添加半个小时,但它一直收到这个错误,我无法修复。如果可以,请帮帮我!!。

完全回溯错误:

代码语言:javascript
复制
Performing hot restart...
Syncing files to device AOSP on IA Emulator...
Restarted application in 797ms.
E/flutter (31824): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: FormatException: Invalid radix-10 number (at character 1)
E/flutter (31824): 5:3
E/flutter (31824): ^
E/flutter (31824): 
E/flutter (31824): #0      int._throwFormatException (dart:core-patch/integers_patch.dart:131:5)
E/flutter (31824): #1      int._parseRadix (dart:core-patch/integers_patch.dart:142:16)
E/flutter (31824): #2      int._parse (dart:core-patch/integers_patch.dart:100:12)
E/flutter (31824): #3      int.parse (dart:core-patch/integers_patch.dart:63:12)
E/flutter (31824): #4      WorldTime.getTime (package:world_time_app/services/world_time.dart:24:39)
E/flutter (31824): <asynchronous suspension>
E/flutter (31824): #5      _LoadingState.SetupWorldTime (package:world_time_app/pages/loading.dart:14:3)
E/flutter (31824): <asynchronous suspension>
E/flutter (31824): 

代码:

代码语言:javascript
复制
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'dart:convert';

class WorldTime{

  String location;
  String time;
  String flag;
  String url;

  WorldTime({this.location, this.flag, this.url});

  Future<void> getTime() async{

    Response response = await get('https://www.worldtimeapi.org/api/timezone/$url');
    Map data = jsonDecode(response.body.toString());
    //print(data);
    String datetime = data['datetime'];
    String offset = data['utc_offset'].substring(2,5);
    //print(dataTime);
    //print(offset);
    DateTime now = DateTime.parse(datetime);
    now = now.add(Duration(hours: int.parse(offset)));

    time = now.toString();
    print(time);



  }

}
EN

回答 2

Stack Overflow用户

发布于 2020-12-31 02:51:48

您试图将5:3解析为int,这会导致错误。

问题出在返回无效数字(5:3)的子字符串中。

首先,您应该执行data['utc_offset'].substring(1,3)以仅获取小时数,执行data['utc_offset'].substring(4,6)以仅获取分钟数。

之后,您可以随心所欲地进行操作,例如:

代码语言:javascript
复制
String offsetHours = data['utc_offset'].substring(1, 3);
String offsetMinutes = data['utc_offset'].substring(4, 6);

DateTime now = DateTime.parse(datetime);
now = now.add(Duration(
  hours: int.parse(offsetHours), 
  minutes: int.parse(offsetMinutes)),
);
票数 1
EN

Stack Overflow用户

发布于 2021-04-18 01:51:03

在我的例子中,我在authority参数中传递了子路由,如下所示:

代码语言:javascript
复制
var response =
          await http.get(Uri.https('https://favqs.com/api/quotes/', '4'), headers: {
        'Content-Type': 'application/json',
        'Authorization': "Token token='xxxxxxxxxxxxxxxx'",
      });

但是在删除第二个参数中的https并移动/api/quotes/之后,这个问题就解决了。

代码语言:javascript
复制
var response =
          await http.get(Uri.https('favqs.com', '/api/quotes/4'), headers: {
        'Content-Type': 'application/json',
        'Authorization': "Token token='xxxxxxxxxxxxxxxx'",
      });
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65511568

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档