Thibaut FAURE
Thibaut FAURE

Reputation: 119

How to display each values map in ListTile?

My goal:
I have a map named 'score' and i would like to display each value in ListTile. The problem that i have this error message: "The expression here has a type void and can't be used."

My code:

...
final Map<String, int> score;
...
return Scaffold(
  appBar: AppBar(
    title: Text('Result'),
  ),
  body: Container(

    child: SingleChildScrollView(
      padding: const EdgeInsets.all(16.0),
      child: Column(
        children: <Widget>[

         score.forEach((playerName, point) => ListTile(
         contentPadding: const EdgeInsets.all(16.0),
         title: Text(playerName),
         trailing: Text(point.toString()),

))

Upvotes: 0

Views: 1705

Answers (2)

LonelyWolf
LonelyWolf

Reputation: 4392

You can create a method...

  final Map<String, int> score = {'playe1': 1, 'player2': 2};

  List<Widget> builder() {
    List<Widget> l = [];
    score.forEach((k, v) => l.add(ListTile(
          contentPadding: const EdgeInsets.all(16.0),
          title: Text(k),
          trailing: Text(v.toString()),
        )));
    return l;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(body: Column(children: builder()));
  }

Upvotes: 1

FloW
FloW

Reputation: 1266

return Scaffold(
    appBar: AppBar(
      title: Text('Result'),
    ),
    body: Container(
        child: SingleChildScrollView(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            children:
              score.entries.map( (entry) => new ListTile(
                contentPadding: const EdgeInsets.all(16.0),
                title: Text(entry.key),
                trailing: Text(entry.value.toString()),
              )).toList(),
          ),
        )
    )
);

Upvotes: 2

Related Questions