1

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 :(

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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'),
      ),
    ],

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.