【问题标题】:Flutter: Converting map api data to listFlutter:将地图 api 数据转换为列表
【发布时间】:2022-11-12 17:37:25
【问题描述】:

当我发出 fetch 请求时,接收到的数据是 map 类型的,但我需要能够将其转换为 list 类型,我该怎么做?

Future<AltinModel> altinGetir() async {
    String url = 'https://finans.truncgil.com/v2/today.json';
    final res = await http.get(Uri.parse(url));

    if (res.statusCode != 200) {
      throw Exception('error');
   }

   var altin = utf8.decode(res.body.runes.toList());
   var js = json.decode(altin);
   var gram_altin = json.decode(res.body);

  return AltinModel.fromJson(js);

 }

模型类 模型类 模型类 模型类

  // To parse this JSON data, do
  //
  //     final altinModel = altinModelFromJson(jsonString);

  import 'dart:convert';

  AltinModel altinModelFromJson(String str) =>
       AltinModel.fromJson(json.decode(str));

  String altinModelToJson(AltinModel data) => json.encode(data.toJson());

class AltinModel {
   AltinModel({
    required this.updateDate,
    required this.onsAltin,
    required this.gramAltin,
    required this.ceyrekAltin,
    required this.yarimAltin,
    required this.tamAltin,
    required this.cumhuriyetAltini,
    required this.ataAltin,
    required this.resatAltin,
    required this.hamitAltin,
    required this.ikibucukAltin,
    required this.gremseAltin,
    required this.besliAltin,
    required this.the14AyarAltin,
    required this.the18AyarAltin,
    required this.the22AyarBilezik,
    required this.gumus,
    });

  DateTime updateDate;
  The14AyarAltin onsAltin;
  The14AyarAltin gramAltin;
  The14AyarAltin ceyrekAltin;
  The14AyarAltin yarimAltin;
  The14AyarAltin tamAltin;
  The14AyarAltin cumhuriyetAltini;
  The14AyarAltin ataAltin;
  The14AyarAltin resatAltin;
  The14AyarAltin hamitAltin;
  The14AyarAltin ikibucukAltin;
  The14AyarAltin gremseAltin;
  The14AyarAltin besliAltin;
  The14AyarAltin the14AyarAltin;
  The14AyarAltin the18AyarAltin;
  The14AyarAltin the22AyarBilezik;
  The14AyarAltin gumus;

    factory AltinModel.fromJson(Map<String, dynamic> json) => AltinModel(
      updateDate: DateTime.parse(json["Update_Date"]),
      onsAltin: The14AyarAltin.fromJson(json["ons_altin"]),
      gramAltin: The14AyarAltin.fromJson(json["gram_altin"]),
      ceyrekAltin: The14AyarAltin.fromJson(json["ceyrek_altin"]),
      yarimAltin: The14AyarAltin.fromJson(json["yarim_altin"]),
      tamAltin: The14AyarAltin.fromJson(json["tam_altin"]),
      cumhuriyetAltini: The14AyarAltin.fromJson(json["cumhuriyet_altini"]),
      ataAltin: The14AyarAltin.fromJson(json["ata_altin"]),
      resatAltin: The14AyarAltin.fromJson(json["resat_altin"]),
      hamitAltin: The14AyarAltin.fromJson(json["hamit_altin"]),
      ikibucukAltin: The14AyarAltin.fromJson(json["ikibucuk_altin"]),
      gremseAltin: The14AyarAltin.fromJson(json["gremse_altin"]),
      besliAltin: The14AyarAltin.fromJson(json["besli_altin"]),
      the14AyarAltin: The14AyarAltin.fromJson(json["14_ayar_altin"]),
      the18AyarAltin: The14AyarAltin.fromJson(json["18_ayar_altin"]),
      the22AyarBilezik: The14AyarAltin.fromJson(json["22_ayar_bilezik"]),
      gumus: The14AyarAltin.fromJson(json["gumus"]),
    );

  Map<String, dynamic> toJson() => {
    "Update_Date": updateDate.toIso8601String(),
    "ons_altin": onsAltin.toJson(),
    "gram_altin": gramAltin.toJson(),
    "ceyrek_altin": ceyrekAltin.toJson(),
    "yarim_altin": yarimAltin.toJson(),
    "tam_altin": tamAltin.toJson(),
    "cumhuriyet_altini": cumhuriyetAltini.toJson(),
    "ata_altin": ataAltin.toJson(),
    "resat_altin": resatAltin.toJson(),
    "hamit_altin": hamitAltin.toJson(),
    "ikibucuk_altin": ikibucukAltin.toJson(),
    "gremse_altin": gremseAltin.toJson(),
    "besli_altin": besliAltin.toJson(),
    "14_ayar_altin": the14AyarAltin.toJson(),
    "18_ayar_altin": the18AyarAltin.toJson(),
    "22_ayar_bilezik": the22AyarBilezik.toJson(),
    "gumus": gumus.toJson(),
      };
   }

  class The14AyarAltin {
    The14AyarAltin({
     required this.buying,
     required this.selling,
     required this.type,
     required this.name,
   });

 String buying;
 String selling;
 String type;
 String name;

factory The14AyarAltin.fromJson(Map<String, dynamic> json) => The14AyarAltin(
    buying: json["Buying"],
    selling: json["Selling"],
    type: json["Type"],
    name: json["Name"],
   );



Map<String, dynamic> toJson() => {
      "Buying": buying,
      "Selling": selling,
      "Type": type,
      "Name": name,
     };


  }

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    你能指定预期用途吗?你想要你的地图列表,还是你的地图从你的 toJson() 方法转换成一个列表?

    如果您只想将地图字段转换为列表,您可以简单地制作一个表示条目的模型

    class MapField{
      late String key;
      late dynamic field;
    
      MapField({
        required this.key,
        required this.field
      });
    }
    

    接着

    var list;
    map.forEach((k, v) => list.add(MapField(k, v)));
    

    但是如果你只想创建一个对象列表。最简单的方法是创建一个模型,将您的地图作为构造函数,并为每个地图返回,将一个对象添加到您的列表中。

    但我再次不确定我是否理解这一点。

    【讨论】:

      猜你喜欢
      • 2023-01-13
      • 2021-06-16
      • 1970-01-01
      • 2011-11-01
      • 1970-01-01
      • 2021-08-29
      • 2020-07-06
      • 1970-01-01
      相关资源
      最近更新 更多