Dynamic class method invocation in Dart

前端 未结 3 1524
心在旅途
心在旅途 2020-12-10 13:07

Like the question at Dynamic class method invocation in PHP I want to do this in Dart.

var = \"name\";
page.${var} =          


        
3条回答
  •  时光取名叫无心
    2020-12-10 13:39

    You can use Dart Mirror API to do such thing. Mirror API is not fully implemented now but here's how it could work :

    import 'dart:mirrors';
    
    class Page {
      String name;
    }
    
    main() {
      final page = new Page();
      var value = "value";
    
      InstanceMirror im = reflect(page);
      im.setField("name", value).then((_){
        print(page.name); // display "value"
      });
    }
    

提交回复
热议问题