(Dart) How to execute each Function in a List?

Viewed 2393

TL;DR: I'm trying to pass a List of Functions to another class, which should then execute every Function.

Why? I need to respond to App Intents in Flutter. My idea was to separate that logic into another Widget, which then gets passed a List of Functions. Upon registering the App onResume event, it should execute the corresponding functions.

How? Currently, my model looks like this:

class LifecycleMethods {
  final List<void> onPaused;
  final List<void> onResumed;
  final List<void> onInactive;
  final List<void> onSuspending;

  LifecycleMethods(
      {this.onPaused, this.onInactive, this.onResumed, this.onSuspending});
}

Which gets passed to this class: (Inspired by this article)

class AppStateObserver extends StatefulWidget {
  final Widget child;
  final LifecycleMethods lifecycleMethods;

  const AppStateObserver({Key key, @required this.child, this.lifecycleMethods})
      : super(key: key);

  @override
  _AppStateObserverState createState() => _AppStateObserverState();
}

class _AppStateObserverState extends State<AppStateObserver>
    with WidgetsBindingObserver {
  final Widget child;
  final LifecycleMethods lifecycleMethods;

  _AppStateObserverState({@required this.child, this.lifecycleMethods});

  @override
  Widget build(BuildContext context) => child;

  @override
  void initState() {
    WidgetsBinding.instance.addObserver(this);
    super.initState();
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.paused) {
      // Execute every item in lifecycleMethods.onPaused
    }
  }
}

The WidgetBindingObserver seems to be the only way to access the App lifecycle. I'm storing the Methods inside the lifecycleMethods variable. Inside the didChangeAppLifecycleState, it should then finally execute the corresponding functions.

Now - How do I properly execute functions inside a list? Is that even possible? Please correct me, if there is a cleaner, better way of solving this problem!

Thx

1 Answers

The problem is, that you have List<void>. You need to change it to List<Function>.

class LifecycleMethods {
  final List<Function> onPaused;
  final List<Function> onResumed;
  final List<Function> onInactive;
  final List<Function> onSuspending;

  LifecycleMethods(
      {this.onPaused, this.onInactive, this.onResumed, this.onSuspending});
}

funcOne() => print('one');

funcTwo() { 
  print('two');
}


...

LifecycleMethods methods = LifecycleMethods(onPaused: [funcOne,funcTwo,()=>print('func anonym')]);
methods.onPaused.forEach((func) => func()); //run every function in onPaused

...

This assumes that you have void Functions without parameters. If your functions take a String and return a bool for example change them to:

...
List<bool Function(String a)> onPaused;
...

or

...
typedef MyFunc = bool Function(String a)
List<MyFunc> onPaused;
...
Related