Flutter shows me the following error message although I converted map
into List
The argument type 'List<Set<Card>>' can't be assigned to the parameter type 'List<Widget>'.dartargument_type_not_assignable
May you please tell me what's wrong?
class MyHomePage extends StatelessWidget {
MyHomePage({Key? key, required String title}) : super(key: key);
final List<Transaction> transactions = [
Transaction(
id: 'sh1',
date: DateTime.now(),
title: 'School',
value: 1554.5
),
Transaction(
id: 'sc5',
date: DateTime.now(),
title: 'Sucar',
value: 16.5
),
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("title"),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const Card(
color: Color.fromRGBO(200, 165, 245, 1.0),
child: Text('[CHART]'),
),
Column(
children:
/////// ERROR
transactions.map( (e) => { return Card(child: Text(e.title),); } ).toList(),
),
],
),
);
}
}
CodePudding user response:
This:
transactions.map( (e) => { return Card(child: Text(e.title),); } ).toList(),
should probably be
transactions.map((e) => Card(child: Text(e.title))).toList(),
or if you prefer the long form:
transactions.map((e) { return Card(child: Text(e.title),); } ).toList(),
But you cannot mix them, otherwise the compiler will interpret the superfluous {} as an intent to have a Set<>
of your items returned instead of a single one.
CodePudding user response:
Dont use =>
and return
togenther. =>
is a shorthand for return and it doenst need curly braces {} to define the method boundary.
transactions.map((e) => Card(child: Text(e.title))).toList(),
This is the preferred way.