Flutter SearchDelegate如何在buildResults中显示数据

时间:2020-06-09 20:46:37

标签: flutter

  1. 你好,我是新手,我开始做新闻应用程序,在搜索文章时我需要帮助来显示文章。

    @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.",
                ),
              )
            ],
          );
        }
      }
    
  2. 这是我获取数据的方式:

    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);
        }
      });
    }
    

    } }

0 个答案:

没有答案