@@ -11,24 +11,28 @@ class WorldTime {
1111 WorldTime ({ this .location, this .flag, this .url });
1212
1313 Future <void > getTime () async {
14- // make the request
15- Response response = await get ('http://worldtimeapi.org/api/timezone/$url ' );
16- Map data = jsonDecode (response.body);
1714
18- // get properties from json
19- String datetime = data['datetime' ];
20- String offset = data['utc_offset' ].substring (1 ,3 );
15+ try {
16+ // make the request
17+ Response response = await get ('http://worldtimeapi.org/api/timezone/$url ' );
18+ Map data = jsonDecode (response.body);
2119
22- // create DateTime object
23- DateTime now = DateTime . parse ( datetime) ;
24- now = now. add ( Duration (hours : int . parse (offset)) );
20+ // get properties from json
21+ String datetime = data[ ' datetime' ] ;
22+ String offset = data[ 'utc_offset' ]. substring ( 1 , 3 );
2523
26- print (now);
24+ // create DateTime object
25+ DateTime now = DateTime .parse (datetime);
26+ now = now.add (Duration (hours: int .parse (offset)));
2727
28- // set the time property
29- time = now.toString ();
30- }
28+ // set the time property
29+ time = now.toString ();
30+ }
31+ catch (e) {
32+ print (e);
33+ time = 'could not get time' ;
34+ }
3135
32- }
36+ }
3337
34- // WorldTime instance = WorldTime(location: 'Berlin', flag: 'germany.png', url: 'Europe/Berlin');
38+ }
0 commit comments