chichi
chichi

Reputation: 3292

Flutter: CupertinoPicker to trigger functions inside the List

I am trying to trigger functions inside the List with the CupertinoPicker.

var _aa = [
      () {
        print('hello1!');
      },
      () {
        print('hello2!');
      },
      () {
        print('hello!3');
      },
    ];

Trying to execute _aa's functions. However, when I try to use it inside the CupertinoPicker, I get Avoid using unnecessary statements. statement.

   CupertinoPicker(
   backgroundColor: Colors.white,
   onSelectedItemChanged: (i) {
                         print(i);
                         _aa[i]; <--- error statement
                        },
                        itemExtent: 32.0,
                        children: List.generate(
                          _aa.length,
                          (i) {
                            print(i);
                          },
                        ),
                      ),

How can I make this work?

Upvotes: 0

Views: 45

Answers (1)

pasty
pasty

Reputation: 420

List<Function> _aa = [
      () {
        print('hello1!');
      },
      () {
        print('hello2!');
      },
      () {
        print('hello!3');
      },
    ];

You forgot to add .call(), like this:

onSelectedItemChanged: (i) {
                         print(i);
                         _aa[i].call();
                        },

Upvotes: 1

Related Questions