Honza Svasek
Honza Svasek

Reputation: 21

Using a list to access a function in dart flutter

How do I access a function through a list?

I have something like this:

  List processList = [
    [001, 'Volume', 0.8,0,0],
    [002, 'Complexity', 0.5,0,0],
    [003, 'Silence', 0.3,0,0],
    [004, 'Notice', 0.3,0,0],
    ....];

And I want 001, 002 etc to be functions that I can call through

processList[i][0] ();

Like a list of functions...

I cannot figure out the syntax for this somehow, the only thing I can achieve is filling the List with the result of the function, but that is not what I need.

Upvotes: 2

Views: 2367

Answers (1)

Richard Heap
Richard Heap

Reputation: 51682

Try

void main() {
  List processList = [
    [(){print('London');}, 'Volume', 0.8, 0, 0],
    [(){print('Aarhus');}, 'Complexity', 0.5, 0, 0],
    [(){print('Munich');}, 'Silence', 0.3, 0, 0],
    [(){print('LA');}, 'Notice', 0.3, 0, 0],
  ];
  int i = 3;
  processList[i][0]();
}

prints LA. Looks at typedefs for ways to declare function signatures.

Upvotes: 3

Related Questions