如何在Dart中将Class对象转换为数据结构“ Map”或“ Maps List”? [英] How to convert Class object to data structure `Map` or a `List of Maps` in Dart?

查看:500
本文介绍了如何在Dart中将Class对象转换为数据结构“ Map”或“ Maps List”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 Object 类型转换为 Map Maps列表 Dart 中,因此变量成为键/值对

How to convert an Object type to a Map or a List of Maps in Dart, so the variables become key/value pairs?

推荐答案

根据我的经验,dart尚未提供这种系统。因此,基本上,我们创建了类似于 toMap()的函数,该函数将对象手动转换为地图的键值对。

Based on my experience, dart does not provide that kind of system yet. So, basically we create function like toMap() that manually convert the object to a key-value pair of map.

例如:

class Human {
  String name;
  int age;

  Map<String, dynamic> toMap() {
    return {
      'name': name,
      'age': age,
    };
  }
}

因此,稍后当您有人类对象,您只需调用 human.tomap()

So, later when you have a Human object, you just can call human.tomap().

我在大多数实体类中都这样做。

I do this in most of my entity classes.

这篇关于如何在Dart中将Class对象转换为数据结构“ Map”或“ Maps List”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆