FLUTTER How to get variable based on passed string name?

前端 未结 3 1163
北荒
北荒 2021-01-13 05:32

I have stored variables in a class with their code names.

Suppose I want to get XVG from that class, I want to do

String getIconsURL(String symbol)          


        
3条回答
  •  北海茫月
    2021-01-13 05:48

    I'd just implement a getProperty(String name) method or the [] operator like:

    class URLsList{
      var XVG = 'some url';
      var BTC = 'some url';
    
      String get operator [](String key) {
        switch(key) {
          case 'XVG': return XVG;
          case 'BTC': return BTC;
        }
      }
    }
    
    String getIconsURL(String symbol) {
      var list = new URLsList();
      return list[symbol];
    }
    

    You can also use reflectable package that enables you to use reflection-like code by code generation.

提交回复
热议问题