Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart strong mode: error on anonymous function return

Tags:

dart

I get the following error. The argument type 'dynamic' can't be assigned to the parameter type '() -> dynamic' The example is:

outerFunc(somevar) {
    return () {....} 
}
anOtherFunction(func()) {....}

anOtherFunction(outerFunc('test'));

These occurs when I return an anonymous function, in strong mode with the analysis_options.yaml on.

strong-mode:
  implicit-casts: false
like image 299
fredtma Avatar asked Jul 16 '26 23:07

fredtma


1 Answers

outerFunc doesn't specify a return type, therefore dynamic is assumed. You can create a typedef and use it as return type for outerFunc. The function type can't be inferred from the return statement.

typedef dynamic F();

F outerFunc(somevar) {
  return () {};
}

You can also write the function type in-line

dynamic Function() outerFunc(somevar) {
  return () {};
}
like image 143
Günter Zöchbauer Avatar answered Jul 22 '26 07:07

Günter Zöchbauer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!