Dart - named parameters using a Map

前端 未结 1 1931
死守一世寂寞
死守一世寂寞 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<Symbol, dynamic>();
      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<Symbol, dynamic> symbolizeKeys(Map<String, dynamic> map){
      final result = new Map<Symbol, dynamic>();
      map.forEach((String k,v) { result[new Symbol(k)] = v; });
      return result;
    }
    
    0 讨论(0)
提交回复
热议问题