如何将searchDelegate与JSON中的将来列表一起使用

时间:2019-02-12 15:40:37

标签: flutter

我正在尝试使用showSearch和searchDelegate在Newsview.org中使用带有future和json的列表视图进行搜索。我已经在这里找到了答案:Flutter - Pass a Future List to a SearchDelegate,但由于我的代码完全不同,因此无法解决问题。

我已尝试通过教程和https://www.youtube.com/watch?v=Wm3OiFBZ2xI&t=2449s使用searchDelegate,并编写了大量逻辑。

这是json;     字符串问题;

Future<List<Article>> searchNews(String source) async {
  final response = await http.get(
      'https://newsapi.org/v2/top-headlines?q= 
   {$question}&apiKey=${API_KEY}');

  sourcequery = json.decode(response.body)['articles']['title'];
  return sourcequery.map((article) => new 
Article.fromJson(article)).toList();
}

我希望问题变量等于searchDelegate中的查询字符串:

class DataSearch extends SearchDelegate<String> {
  List titles = sourcequery;

  @override
  List<Widget> buildActions(BuildContext context) {
    return [
      IconButton(
        icon: Icon(Icons.clear),
        onPressed: () {
          query = question;
        },
      )
    ];
  }

  @override
  Widget buildLeading(BuildContext context) {
    return IconButton(
        icon: AnimatedIcon(
          icon: AnimatedIcons.menu_arrow,
          progress: transitionAnimation,
        ),
        onPressed: () {
          close(context, null);
        });
  }

  @override
  Widget buildResults(BuildContext context) {
    // TODO: implement buildResults
    return null;
  }

  @override
  Widget buildSuggestions(BuildContext context) {
    // final suggestionList = query.isEmpty;
  }
}

showSearch

actions: <Widget>[
         IconButton(
           color: Colors.red,
           icon: Icon(Icons.search),
           tooltip: 'search for news',
           onPressed: () {
             showSearch(context: context, delegate: DataSearch());
           },
         )
       ],

我希望showSearch从json返回一个列表视图

0 个答案:

没有答案