import 'package:flutter/material.dart';
import 'package:http/http.dart' as https;
import 'dart:convert';
//above is for import
class Loading extends StatefulWidget {
const Loading({Key? key}) : super(key: key);
@override
State<Loading> createState() => _LoadingState(); //code here
} //code above
//below code
class _LoadingState extends State<Loading> {
void getTime () async {
//make the request for timezone from Europe,London
Uri url = Uri.parse('https://worldtimeapi.org/api/timezone/Europe/London');
https.Response response = await https.get(url);
Map data = jsonDecode(response.body);
// print(data);
//code above
//get properties from data
String datetime = ['datetime'] as String;
String offset = ['offset'] as String;
print(datetime);
print(offset);
}
@override
void initState() {
super.initState();
getTime();
//code above
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Text('loading screen'),
);
}
}
下面是我的控制台:
Performing hot restart...
Syncing files to device AOSP on IA Emulator...
Restarted application in 1,541ms.
E/flutter (16861): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'List<String>' is not a subtype of type 'String' in type cast
E/flutter (16861): #0 _LoadingState.getTime (package:world_time/pages/loading.dart:22:36)
E/flutter (16861): <asynchronous suspension>
E/flutter (16861):
1条答案
按热度按时间slsn1g291#
问题在这里:
你将把一个List([""])赋值给String。
另外,在Json响应中没有任何名为“offset”的内容。因此,进行以下更改: