Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in Dart, problems with static method when called from variable

have class Klass with static method fn1

class Klass {
  static String fn1() => 'hello';
}

> Klass.fn1(); //  hello

but when Klass is assigned to a variable, calling the method fn1 fails

var k = Klass;

> k.fn1() // "Unhandled exception: Class '_Type' has no instance method 'fn1'.

don't quite know what's going on here

like image 406
cc young Avatar asked Dec 27 '13 02:12

cc young


1 Answers

A simple workaround

class Klass {
  static fn1(String name) {
    return name;
  }
  
  fn1NonStatic(String name) {
    return fn1(name);
  }
}

Klass().fn1NonStatic("test");
like image 159
ITW Avatar answered Sep 29 '22 04:09

ITW