Christer
Christer

Reputation: 3076

Dart - Pass Interface as parameter

I'm looking for a similar solution for passing interface as paremeter in a function like Kotlin has:

test(object: Handler {
    override fun onComplete() {

    }
})

The only thing I find for Dart is using implements on the class, which is fine, but not what I need.

My approach so far has been making abstract class:

abstract class AuthListener{
  void onChange(AuthState authState);
}

Triggering of listener:

isSignedInListener(AuthListener authListener){
    FirebaseAuth.instance
        .authStateChanges()
        .listen((User user) {
      if (user == null) {
        return authListener.onChange(AuthState.SIGNED_OUT);
      } else {
        return authListener.onChange(AuthState.SIGNED_IN);
      }
    });
  }

And now I need to listen for the response, which is where I need some help: enter image description here

Upvotes: 1

Views: 1109

Answers (1)

fartem
fartem

Reputation: 2531

You can create a function and use it as a callback (pass to methods etc.).

Function can be placed into variable:

  Function(AuthState authState) callback;

or as argument:

void isSignedInListener(Function(AuthState authState) callback) {}

auth.isSignedInListener((authState) {
  // do something here
});

Upvotes: 1

Related Questions