Future<List<News>> getNews() async {
final Url =
"https://api.stockdata.org/v1/news/all?symbols=&filter_entities=true&language=en&api_token=api_token&countries=";
final response = await http.get(Uri.parse(Url));
if (response.statusCode == 200) {
List jsonResponse = json.decode(response.body);
return jsonResponse.map((data) => News.fromJSON(data)).toList();
} else {
throw Exception("Unexpected error occurred!");
}
}
class News {
final String title;
final String desc;
final String imgURL;
final String url;
News(
{required this.title,
required this.desc,
required this.imgURL,
required this.url});
factory News.fromJSON(Map<String, dynamic> json) {
return News(
title: json["title"],
desc: json["description"],
imgURL: json["image_url"],
url: json["url"]);
}
}
FutureBuilder<List<News>>(
future: futureNews,
builder: (context, snapshot) {
if (snapshot.hasData) {
List<News> data = snapshot.data; -----> Error
return ListView.builder(),
我正在尝试使用Stockdata API为股票构建一个新闻小部件,该API以JSON格式发送数据,但我一直得到错误:
A value of type 'List<News>?' can't be assigned to a variable of type 'List<News>'.
错误出现在列表data = snapshot.data;
谁能帮我做同样的事呢?能否尝试更新List<News> data = snapshot.requireData;
?