我有名单的型号类型,该名单是用来收集用户的ID和专业. SpecialityList是一个列表.

var specialityList = <SpecialityDatum>[].obs;

SpecialityDatum is Model,

class SpecialityDatum {
  SpecialityDatum({
     this.id,
     this.speciality,
     this.status,
    this.createdAt,
    this.updatedAt,
    this.deletedAt,
  });

  int? id;
  String? speciality;
  int? status;
  DateTime? createdAt;
  DateTime? updatedAt;
  dynamic deletedAt;

  factory SpecialityDatum.fromJson(Map<String, dynamic> json) => SpecialityDatum(
    id: json["id"]??0,
    speciality: json["speciality"]??"",
    status: json["status"]??"",
    createdAt: json["created_at"] != null ? DateTime.parse(json["created_at"]) : null,
    updatedAt: json["updated_at"] != null ? DateTime.parse(json["updated_at"]) : null,
    deletedAt: json["deleted_at"],
  );

  Map<String, dynamic> toJson() => {
    "id": id,
    "speciality": speciality,
    "status": status,
    "created_at": createdAt,
    "updated_at": updatedAt,
    "deleted_at": deletedAt,
  };

  String toStringView(){ // what ever you name it
    return '$speciality'; // if you want to show id and speciality as string
  }

}

我想将该模型列表转换为数组,如下所示.

Output : [{"speciality_id":12,"speciality_name":"orthopedic"},{"speciality_id":13,"speciality_name":"hyoerthing"}]

推荐答案

试试这个:

var result = specialityList.map((e) => e.toJson()).toList();

result将是包含诸如ID、状态和其他特性的 map 列表.

Flutter相关问答推荐

来自另一个屏幕的数据

ListTile未正确显示在flutter中

壁炉有序在Flutter

如何让一个名为ONTAP的回调函数、另一个名为onTapDown的回调函数和另一个名为onTapUp的回调函数不重叠?

相同的Flutter 代码库,但Firebase登录适用于Web应用程序,但无法登录iOS应用程序

Flutter 蜂窝-检索非基元数据类型列表

有没有什么方法可以加快在Flutter 中加载Quill HTML编辑器的速度?

将列表<;Events>;从Flutter 应用程序中的函数传回EventLoader属性时出错

参数类型';List<;Dynamic&>不能分配给参数类型';List<;SingleChildWidget&>';

Firebase WHERE过滤器在日期字段中不起作用

从图标启动器打开时,VSCode未检测到Web设备

在带有 flutter 的 Native Android 中,EventChannel.EventSink 始终为 null

Dart/Flutter 泛型:类型 '(SearchFilterItemModel) => String' 不是类型 '(SearchFilterItemModel) => String' 的子类型

如何从 Flutter 中的 App2 远程点击 App1 中的按钮

是否可以在 flutter 应用程序中使用 jetpack compose?

使用 Play Integrity API 时,Firebase 电话身份验证问题缺少客户端标识符错误

更改底部导航栏下方 Flutter Snackbar 的位置

为什么 ref.watch(otherProvider.stream) 在 Riverpod 2.x 中被弃用并且将在 3.x 中被删除?

Flutter RawMaterialButton 常量相对大小

如何使用 Navigator 2.0 导航到任何页面?