class Screen extends StatefulWidget {
const Screen({Key? key}) : super(key: key);
@override
State<Screen> createState() => _ScreenState();
}
class _ScreenState extends State<Screen> {
@override
void initState() {
// TODO: implement initState
super.initState();
requestUrl();
}
Future<dynamic> requestUrl() async {
List<Response> response;
var dio = Dio();
var newsHome =
'https://newsapi.org/v2/top-headlines?country=kr&apiKey=3e3d6c4106c84f27be6d9578970fc4e8';
response = await Future.wait([
dio.get(newsHome),
]);
print(response[0]['articles']);
}
44:22: Error: The operator '[]' isn't defined for the class 'Response<dynamic>'.
- 'Response' is from 'package:dio/src/response.dart' ('../flutter/.pub-cache/hosted/pub.dartlang.org/dio-4.0.6/lib/src/response.dart').
Try correcting the operator to an existing operator, or defining a '[]' operator.
print(response[0]['articles']);
通过dio库进行http通信时,出现了与上面代码中相同的错误。我找了好几个地方,但我不太确定是什么问题。如有任何帮助,不胜感激。
使用Response
上的数据访问实际数据。像这样:
response[0].data['articles']