你好,我是新手,我开始做新闻应用程序,在搜索文章时我需要帮助来显示文章。
@override Widget buildResults(BuildContext context) {
// show some result based on the selection
if (query.length < 3) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Center(
child: Text(
"Search term must be longer than two letters.",
),
)
],
);
}
}
这是我获取数据的方式:
class SearchNewsClass {
List<SearchModel> searches = [];
Future<void> getSearches(String query) async {
String url ="https://newsapi.org/v2/everything?q=$query&sortBy=popularity&apiKey=$_apiKey";
var response = await http.get(url);
var jsonData = jsonDecode(response.body);
if (jsonData['status'] == "ok") {
jsonData["articles"].forEach((element) {
if (element["urlToImage"] != null && element['description'] != null) {
SearchModel searchModel = SearchModel(
title: element["title"],
author: element["author"],
description: element["description"],
url: element["url"],
urlToImage: element["urlToImage"],
content: element["context"],
publishedAt: element["publishedAt"],
);
searches.add(searchModel);
}
});
}
} }