user9644903
user9644903

Reputation: 3

Rebuild a widgets in searchdelegate flutter

I want to rebuild the buildSuggestions method that returns a widget that the defined below and resultInfo getting the result from TMDB API search that a string as an input how can i recall the resultInfo and rebuild it.

  @override
 Widget buildSuggestions(BuildContext context) {
    if (query.length > 1) {
      return ResultInfo(s: query);
    }
    return ResultInfo(s: "old");
    //ResultInfo not updating after the query updates 
  }

this is the home screen code where searchdelegate is defined and here you can find the code for ResultInfo widget.

Upvotes: 0

Views: 1021

Answers (1)

Loren.A
Loren.A

Reputation: 5575

UPDATE: After looking at the project, you need to call the getMovies function from the buildSuggestions build method because initState was only getting called once despite the rebuilds. Add this and you're good.

 @override
  Widget buildSuggestions(BuildContext context) {
    if (query != '') {
      searchResult.getMovies(query); // do the search from here
      return ResultInfo(searchString: query);
    }
    return ResultInfo(searchString: "old");
  }
}

You can leave the search in initState also so you don't get a blank list when you navigate to the search page.

OLD ANSWER:

Any time the query changes in a widget that extends SearchDelegate, buildSuggestions is called. You can verify that by adding a print statement in the buildSuggestions build method.

I see a couple issues here:

class ResultInfo extends StatefulWidget {
  final String s;
  ResultInfo({Key key, @required this.s}) : super(key: key);
  @override
  _MovieInfoState createState() => _MovieInfoState(s);
}

class _MovieInfoState extends State<ResultInfo> {
  // final String s;  don't need this here
  _MovieInfoState(this.s);
  @override
  void initState() {
    super.initState();
    searchResult.getMovies(widget.s); // access the actual parameter you passed in with widget.s 
  }

When you pass a value into a stateful widget you don't re-declare the same value again in the stateful part, you access what was passed in with widget.variable. So in your case you were passing in a null value into the getMovies function.

So assuming your stream functionality is all working, that simple change alone should fix your issue.

And in the interest of your code being readable by other people, I suggest a better variable name than s, like query for example because that's what it is. Anyone reading your code has no idea what s is and what it's for until they dig around a bit.

Second issue:

if (query.length > 1) { // won't return results until 2nd character is input
      return ResultInfo(s: query);
    }
    return ResultInfo(s: "old");
  }

Maybe its your intention to only return ResultInfo(s: query) until more than one character is input, but if its not, change your condition to this

if (query != '') {
      return ResultInfo(s: query);
    }
    return ResultInfo(s: "old");
  }

Upvotes: 1

Related Questions