Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.7k views
in Technique[技术] by (71.8m points)

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

I was following this flutter beginner tutorial from net ninja here .But my timezone UTC +5:30 in Sri Lanka so i tried to add the half an hour but it keep getting this error that I cant fix. Please help me if you can!!.

Full Traceback error:

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): 

Code :

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



  }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are trying to parse 5:3 as int which causes the error.

The issue is in the substring that is returning an invalid number (5:3). You should do data['utc_offset'].substring(1,3) to get only the hour and data['utc_offset'].substring(4,6) to get only the minutes. After that you can manipulate as you wish, like:

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)),
);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...