dart - Flutter - 遍历对象键和值

我有和反对,我想知道是否有一种简单的方法来遍历它的键和值?

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/

相关文章:

flutter - 检测用户是否离开 Flutter 中的当前页面?

ios - Flutter 在我的 Flutter 应用程序的根目录上构建 IOS 不起作用

dart - 在 Flutter 中获取设备的宽度和高度

android-studio - Android : Could not find aapt2-pr

ios - 将 watch 应用程序添加到 Flutter iOS 应用程序

flutter - 标题在flexibleSpaceBar上没有正确显示

flutter - Flutter 中的可滚动底部工作表

dart - 检查无状态小部件是否在 flutter 中处理

dart - Flutter getter 没有为类指定,当它被指定时

android - 在 Flutter 中剪切文本效果