dartproxy-object

dart proxy objects can or cannot be assigned to typed variables?


I have the following test code:

@proxy
class A{
  noSuchMethod(Invocation inv) => "no problems";
}

class B{
  String get aString => "I'm a B string";
}

void main() {
  B b = new A();
  print(b.aString);
}

from what I read on the api site about proxy, I assumed it would be ok to assign a proxy to anything without getting a TypeError at runtime, but that isn't the case here. What is the point in having proxies implement essentially anything they want if they can't be assigned to anything without getting TypeErrors thrown. in the docs it says it isnt a static type error to assign a proxy to any variable type.


Solution

  • @proxy is used to avoid warnings.

    class A{
      noSuchMethod(Invocation inv) => "no problems";
    }
    
    @proxy
    class B{
      noSuchMethod(Invocation inv) => "no problems";
    }
    
    void main() {
      A a = new A();
      B b = new B();
    
      a.something; // warning
      b.something; // no warning
    }