Dart - named parameters using a Map

前端 未结 1 1930
死守一世寂寞
死守一世寂寞 2021-01-11 09:36

I would like to know if I can call a function with name parameters using a map e.g.

void main()
{
  Map a = {\'m\':\         


        
1条回答
  •  伪装坚强ぢ
    2021-01-11 10:32

    You can use Function.apply to do something similar :

    main() {
      final a = new Map();
      a[const Symbol('m')] = 'done';
      Function.apply(slave, [], a);
    }
    

    You can also extract an helper method to simplify the code :

    main() {
      final a = symbolizeKeys({'m':'done'});
      Function.apply(slave, [], a);
    }
    
    Map symbolizeKeys(Map map){
      final result = new Map();
      map.forEach((String k,v) { result[new Symbol(k)] = v; });
      return result;
    }
    

    0 讨论(0)
提交回复
热议问题