我有和反对,我想知道是否有一种简单的方法来遍历它的键和值?
class Post {
String id;
String title;
String article;
Post(
{
this.id,
this.title,
this.article
}
);
}
最佳答案
没有。
您要求的是反射(reflection)。您可以使用 dart:mirrors
如果该库在您的平台上可用(它不在 Flutter 中),则用于此的 API。
您也许可以使用 package:reflectable
为其生成代码。 .
或者您可以使用 package:json_serializable
生成 Map
来自一个对象。
如果您只是想为一个特定的类(class)做某事,我会手动
写:
dart
Map<String, dynamic> toJson() => {"id": id, "title": title, "article": article};
然后在你想迭代时使用它。
https://stackoverflow.com/questions/53629646/