mfrachet
mfrachet

Reputation: 8922

Error while passing data to flutter child

I'm trying to learn Google Flutter and I m having an issue while trying to pass a data to a child .

For now, I m having two widgets : one that should display a list of pokemons, and one representing a pokemon.

From the ListPokemon one, I'm creating each line using :

  List<Pokemon> _createRow() {
    return this
        ._pokemons
        .map((pokemonData) => new Pokemon(pokemonName: 'Super pokemon name'))
        .toList();
  }

From the PokemonCard, I've tried to make something like :

class Pokemon extends StatelessWidget {
  Pokemon({Key key, this.pokemonName}) : super(key: key);

  final String pokemonName;

  @override
  Widget build(BuildContext context) {
    print(pokemonName); // prints the argument
    return new Card(
      child: new Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          const ListTile(
            leading: const Icon(Icons.album),
            title: const Text(pokemonName), // gives : Arguments of constant creation must be constant expressions
            subtitle: const Text('That is a weird pokemon :O'),
          ),
        ],
      ),
    );
  }
}

My problem is that something is going wrong right here :

title: const Text(pokemonName), // gives : Arguments of constant creation must be constant expressions

And I don't understand why.

What I wanted was simply to pass a string down to the child widget and display it on the screen.

Can anybody help me understand this error ?

EDIT : I've try to move const Text => new Text. Same thing occurs :(

Upvotes: 1

Views: 2042

Answers (2)

Chan Yiu Tsun
Chan Yiu Tsun

Reputation: 21

I encountered this problem too. The main problem is you define the "ListTile" as "const". Remove "const" should make your code work.

Upvotes: 2

Maximilian Riegler
Maximilian Riegler

Reputation: 23506

Instead of adding const widgets, instantiate them with new. Since you're having dynamic content, they can't be const anyway :)

    children: <Widget>[
      new ListTile(
        leading: const Icon(Icons.album),
        title: new Text(pokemonName), 
        subtitle: const Text('That is a weird pokemon :O'),
      ),
    ],

Upvotes: 0

Related Questions