I’m playing arround with Flutter and the first app demo from their codelab. I don’t understand why this doesn’t work:
class HistoryList extends StatelessWidget {
@override
Widget build(BuildContext context) {
var appState = context.watch<MyAppState>();
if (appState.history.isEmpty) {
return Center(
child: Text('No history yet.'),
);
} else {
return ListView(
children: [
for (var hist in appState.history)
ListTile(
leading: Icon(Icons.favorite),
title: Text(hist.asLowerCase),
),
],
);
}
}
}
but this works:
class HistoryList extends StatelessWidget {
@override
Widget build(BuildContext context) {
var appState = context.watch<MyAppState>();
if (appState.history.isEmpty) {
return Center(
child: Text('No history yet.'),
);
} else {
return Text(appState.history.firstOrNull.toString());
}
}
}
I’m not getting anything in VSCode debug console but the app just crashes when I try to return a ListView instead of a Text.
Thanks for any help
2
Answers
For anyone visiting this I solved it.
The problem is not how the ListView is written but where it was called from, it has to be wrapped in something in order for it to occupy the correct space in a column or row (for example a SizedBox or an expanded)
i am not sure about the code .However if you use ternary operator ,it will be better .Just suggesting