I would like a member of a class to received a listner from its owner class. So, something like this:
class Member {
Member({required this.listener});
final VoidCallback listener;
}
class Owner {
final member = Member(listener: saySomething); // <- error here
void saySomething() {
debugPrint('hello');
}
}
But I get the error on listener: saySomething
that reads instance member can't be accessed in an initializer
. My understanding is that it's because the compiler builds the Member instance first and then the Owner instance, so doesn't have the locations in memory yet.
I know I can do this in two steps. E.g., instantiate member
and then assign its listener
in my constructor or wherever, but it would be really nice if I could assign listener
when member
is instantiated.
I'm pretty sure that's not possible but am hoping to be proven wrong?