How to convert JavaScript object to Dart Map?

后端 未结 5 642
梦毁少年i
梦毁少年i 2021-01-04 02:24

I am looking for some reverse of new JsObject.jsify. Something, that would convert javascript Object back to Dart Map Is there somethi

5条回答
  •  -上瘾入骨i
    2021-01-04 02:58

    If you want to do it deeply and handle also other cases then simple maps AND preserve functions (unlike the json solution) then use this simple function:

    _toDartSimpleObject(thing) {
     if (thing is js.JsArray) {
      List res = new List();
      js.JsArray a = thing as js.JsArray;
      a.forEach((otherthing) {
        res.add(_toDartSimpleObject(otherthing));
      });
      return res;
    
     } else if (thing is js.JsObject) {
      Map res = new Map();
      js.JsObject o = thing as js.JsObject;
      Iterable k = js.context['Object'].callMethod('keys', [o]);
       k.forEach((String k) {
       res[k] = _toDartSimpleObject(o[k]);
      });
      return res;
    
     } else {
      return thing;
     }
    }
    

提交回复
热议问题