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