I am trying to make a Music Playing App. And in that app i want to implement search feature. This requires me to show list of 5 answers related to query only if the user is trying to search, else do not.
I tried to use FutureBuilder inside if statement as i am using API, but it did not work. I have tried building separate widget and then calling it inside the if statement but to no avail. Then i tried to display a simple container inside if statement but it would not show.
Here is the code:
onChanged: (value) async {
final String accessToken =
await jiosaavn.getaccesstoken();
if (selectedValue == "Artists" && value != '')
Container(
width: double.infinity,
height: double.infinity,
color: Colors.blue,
);
if (selectedValue == "Albums")
Container(
width: double.infinity,
height: double.infinity,
color: Colors.blue,
);
if (selectedValue == "Tracks" && value != '')
Container(
width: double.infinity,
height: double.infinity,
color: Colors.blue,
);
},
Even Container is not displayed. Is it possible?
2
Answers
The reason why your solution does not work is because the onChanged callback does not return anything, especially not anything to the current widget tree.
Below is a simple stateful widget with a dropdown that will set state, which cause a rerender of the view calling the _buildView() function with the new _selectedItem value, returning the widget you want.
You are trying to display a container inside an
if
statement. However, you cannot display a widget inside anif
statement like that. Instead, you can use aStatefulWidget
and update the state of the widget when the user types in a search query.Here’s an example of how you can do this:
Here, there is a
StatefulWidget
and a boolean_showContainer
to keep track of whether or not to show the container. When the user types in a search query, we update the state of_showContainer
and show the container accordingly.