在Dart/Flutter中的动态类型转换

huangapple go评论64阅读模式
英文:

Dynamic Type Casting in Dart/Flutter

问题

以下是翻译好的部分:

我正在编写一个库,用于在Dart/Flutter中动态序列化/反序列化任何对象(与Python的Pydantic类似的想法)。然而,我发现实现最后一个组件——动态类型转换——几乎不可能。这是为了将从对象中使用反射检索的类型从JSON转换为列表<dynamic>到列表<string>(或类似的类型)。以下是期望的实现(尽我所知,这在Dart中不可能)。

Map<String, Type> dynamicTypes = {"key": int };

// 常规转换将是 "1" 作为 int
int value = "1" as dynamicTypes["key"];

是否有某种解决方法使这个实现成为可能?或者我是否已经陷入了死胡同(因此不存在其他动态序列化/反序列化包)?

英文:

I am in the middle of writing a library to dynamically serialise/deserialise any object in Dart/Flutter (Similar in idea to Pydantic for Python). However, I am finding it impossible to implement the last component, dynamic type casting. This is required in order convert types from JSON, such as List<dynamic> to List<string> (or similar). The types are retrieved from objects using reflection.

The below is the desired implementation (though as far as I understand this is not possible in Dart).

Map&lt;String, Type&gt; dynamicTypes = {&quot;key&quot;: int };

// Regular casting would be &quot;1&quot; as int
int value = &quot;1&quot; as dynamicTypes[&quot;key&quot;];

Is there some workaround which makes this possible to implement? Or have I reached a dead end with this (hence no other dynamic serialisation/deserialisation package already exists).

答案1

得分: 1

Conducting more research into this issue, it seems in Dart's current implementation this is impossible due to runtime reflection being disabled as referenced here in the official docs.

在深入研究这个问题时,似乎在Dart的当前实现中,由于禁用了运行时反射,这是不可能的,如官方文档中所引用的这里

There are ongoing discussions about the support for this and the associated package dart:mirrors here on GitHub, but so far though there is some desire for such functionality it is highly unlikely to ever be implemented.

关于此问题和相关包dart:mirrors的支持,正在GitHub上进行着持续的讨论,但迄今为止,尽管有一些对此功能的渴望,但很不可能被实现。

As a result, the only options are:

  • Use code generation libraries to generate methods.
  • Manual serialisation/deserialisation methods.
  • Implement classes with complex types such as lists and maps to be dynamic, enabling (all be it limited) automatic serialisation/deserialisation.

因此,唯一的选择是:

  • 使用代码生成库生成方法。
  • 手动序列化/反序列化方法。
  • 实现具有复杂类型的类,如列表和映射,使其成为动态的,从而实现(尽管有限)自动序列化/反序列化。
英文:

Conducting more research into this issue, it seems in Dart's current implementation this is impossible due to runtime reflection being disabled as referenced here in the official docs.

There are ongoing discussions about the support for this and the associated package dart:mirrors here on GitHub, but so far though there is some desire for such functionality it is highly unlikely to ever be implemented.

As a result, the only options are:

  • Use code generation libraries to generate methods.
  • Manual serialisation/deserialisation methods.
  • Implement classes with complex types such as lists and maps to be dynamic, enabling (all be it limited) automatic serialisation/deserialisation.

答案2

得分: 0

以下是翻译好的部分:

你的问题没有具体说明 `dynamicTypes` 是如何构建的,或者它的键是如何派生的。所以对于我来说,可能有一些细节不太清楚。

但是像这样的做法如何?

如果你的值是动态的而不是一个字符串:

甚至更简洁的是:
英文:

Your question does not specify how exactly dynamicTypes is built, or how its key is derived. So there is perhaps a detail to this that is not clear to me.

But what about something like this?

class Caster&lt;T&gt; {
  final T Function(String) fromString;
  Caster(this.fromString);
}

void main() {
  Map&lt;String, Caster&gt; dynamicTypes = { &quot;key&quot;: Caster&lt;int&gt;((s) =&gt; int.parse(s)) };
  int v = dynamicTypes[&#39;key&#39;]!.fromString(&#39;1&#39;);
  print(v);
}

Or, if you have the value as a dynamic rather than a string:

class Caster&lt;T&gt; {
  T cast(dynamic value) =&gt; value as T;
}

void main() {
  Map&lt;String, Caster&gt; dynamicTypes = { &quot;key&quot;: Caster&lt;int&gt;() };
  dynamic a = 1;
  int v = dynamicTypes[&#39;key&#39;]!.cast(a);
  print(v);
}

Or even more succinctly:

void main() {
  dynamic a = 1;
  int v = a;
  print(v);
}

huangapple
  • 本文由 发表于 2023年2月16日 08:36:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定