goldilocks
goldilocks

Reputation: 161

Using search delegate for a listview generated from Future

Here is my listview generated from a Future from a json file.

class _ChorusPage extends State<ChorusPage> {
  static Future<List<Chorus>> getList() async {
    var data = await rootBundle.loadString('assets/chorusJson.json');
    var jsonMap = json.decode(data); // cast<Map<String, dynamic>>();

    List<Chorus> choruses = [];
    for (var c in jsonMap) {
      Chorus chorus = Chorus.fromJson(c);
      choruses.add(chorus);
    }
    // var = User.fromJson(parsedJson);
    // choruses = jsonMap.map<Chorus>((json) => Chorus.fromJson(json)).toList();

    print(choruses.length);
    return choruses;
  }

  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(title: new Text('Chorus'), actions: <Widget>[
        IconButton(icon: Icon(Icons.search), onPressed: () {})
      ]),
      body: Container(
          child: FutureBuilder(
              future: getList(),
              builder: (BuildContext context, AsyncSnapshot snapshot) {
                if (snapshot.data == null) {
                  return Container(
                      child: Center(child: CircularProgressIndicator()));
                } else {
                  return ListView.builder(
                      itemCount: snapshot.data.length, // + 1,
                      itemBuilder: (BuildContext context, int index) {
                        return _listItem(index, snapshot);
                      });
                }
              })),
    );
  }

I am trying to implement a search function using the search delegate. The tutorial I am watching searches a List (https://www.youtube.com/watch?v=FPcl1tu0gDs&t=444s). What I have here is a Future. I am wondering how do you convert a future into a List. Or is there any other workaround.

class DataSearch extends SearchDelegate<String> {
  Future<List<Chorus>> chorusList = _ChorusPage.getList();
  // ????????????????????? How do I convert. 

  @override
  List<Widget> buildActions(BuildContext context) {
    // actions for app bar
    return [IconButton(icon: Icon(Icons.clear), onPressed: () {})];
  }

  @override
  Widget buildLeading(BuildContext context) {
    // leading icon on the left of the app bar
    return IconButton(
        icon: AnimatedIcon(
          icon: AnimatedIcons.menu_arrow,
          progress: transitionAnimation,
        ),
        onPressed: () {});
  }

  @override
  Widget buildResults(BuildContext context) {
    // show ssome result based on the selection
    throw UnimplementedError();
  }

  @override
  Widget buildSuggestions(BuildContext context) {
    /*
    final suggestionList = query.isEmpty ? recentChorus : chorus;

    return ListView.builder(
      itemBuilder: (context, index) => ListTile(
        title: Text(suggestList[chorus]),
      ),
      itemCount: suggestionList.length,
    );
    // show when someone searches for
    */
  }
}

Upvotes: 0

Views: 577

Answers (1)

Lucas Vaudey
Lucas Vaudey

Reputation: 131

In my opinion you should set your chorusList and call somewhere your getList method with the .then method store the value inside your chorusList.

List<Chorus> chorusList;
_ChorusPage.getList().then((value) => chorusList);
  

Upvotes: 1

Related Questions