darttypeerrorstatic-analysiszone

Type assignment error in implementing Dart's ZoneSpecification


I'm getting a static analysis error while implementing Dart's Zone.

Code:

void main() {
  Zone.current.fork(specification: ZoneSpecification(scheduleMicrotask: (self, parent, zone, f) => parent.scheduleMicrotask(zone, f),
  run: (self, parent, zone, f) => parent.run(zone, f)));
}

error message:

The argument type 'dynamic Function(dynamic, dynamic, dynamic, dynamic)' can't be assigned to the parameter type 'R Function(Zone, ZoneDelegate, Zone, R Function())?

The error only appears while implementing non-void-return functions run, registeredCallback and not with scheduleMicrotask, createTimer (and remaining void functions)


Solution

  • The type for the run handler is R Function<R>(Zone, ZoneDelegate, Zone, R Function()), which is a generic function. That's why it can run functions returning any type.

    Your code should be:

    void main() {
      Zone.current.fork(specification: ZoneSpecification(scheduleMicrotask: (self, parent, zone, f) => parent.scheduleMicrotask(zone, f),
      run: <R>(self, parent, zone, f) => parent.run(zone, f)));
    }