I'm new to using Flutter/Dart and I'm having a hard time understanding why the following code doesn't update the ListView contents when _updateResults() calls setState(). I've even added a print statement to display the contents of _results and I can see that items are being added to the list but the display never updates.
class SearchHomePage extends StatefulWidget {
SearchHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
createState() => _SearchHomePageState();
}
class _SearchHomePageState extends State<SearchHomePage> {
List<Widget> _results = <Widget>[];
@override
void initState() {
super.initState();
_results.add(Text("testing"));
}
void _updateResults(String text) {
setState(() {
_results.add(Text(text));
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(children: [
Container(
child: TextField(
onSubmitted: (String text) {
_updateResults(text);
},
),
),
Expanded(
child: ListView(children: _results)
),
]),
);
}
}
If I change the ListView portion to be like:
Expanded(
child: ListView(children: _results.toList())
)
Then it will work and I'm not sure why because it was already a list. What exactly is going on here and why doesn't it work with just ListView?