dart 我想让我的json列表equipmentscontrol转换成一个列表EquipmentControl

xyhw6mcr  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(122)

这是我的控制模型:

import 'package:flutter_data/flutter_data.dart';
import 'vehicle.dart';
import 'agent.dart';
import 'equipementcontrol.dart';

class Control {
  @override
  final int id;
  final String date;
  final int nbKm;
  final int fuelLevel;
  final String comments;
  final Vehicle vehicle;
  final Agent agent;
  final List<EquipementControl> equipementcontrols;

  // Constructor, with syntactic sugar for assignment to members.
  Control({
    required this.id,
    required this.date,
    required this.nbKm,
    required this.fuelLevel,
    required this.comments,
    required this.vehicle,
    required this.agent,
    required this.equipementcontrols,
  })
  {
    // Initialization code goes here.

  }
  // Construct an object from a mapped JSON array
  factory Control.fromJson(Map<String,dynamic> mappedJson){
    return Control(
        id: mappedJson['id'],
        date: mappedJson['date'],
        nbKm: mappedJson['nbKm'],
        fuelLevel: mappedJson['fuelLevel'],
        comments: mappedJson['nbKm'],
        vehicle: Vehicle.fromJson(mappedJson['vehicle']),
        agent: Agent.fromJson(mappedJson['agent']),
        equipementcontrols: List<dynamic>.from(mappedJson['equipementcontrols']).map((i) => EquipementControl.fromJson(i)).toList(),
    );
  }
}

这是我的错误:
错误:NoSuchMethodError:'map' C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/* internal/js_dev_runtime/private/ddc_runtime/errors. dart 266:49掷 * C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_ internal/js_dev_runtime/private/ddc_runtime/operations. dart 768:3 defaultNoSuch方法C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_ internal/js_dev_runtime/patch/core_patch. dart 61:方法C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_ internal/js_dev_runtime/private/ddc_runtime/operations. dart 763:方法C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_ internal/js_dev_runtime/private/ddc_runtime/operations. dart 283:12次呼叫NSM C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_ internal/js_dev_runtime/private/ddc_runtime/operations. dart 342:14_checkAndCall C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_ internal/js_dev_runtime/private/ddc_runtime/operations. dart 431:方法C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_ internal/js_dev_runtime/private/ddc_runtime/operations. dart 438:5dgsend packages/campus/model/control_rao.dart35:38 latestControlFromOneVehicle C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_ internal/js_dev_runtime/patch/async_patch. dart 45:50 ℃:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/zone. dart 1660:54 runUnary C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl. dart 147:18 handleValue C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl. dart 767:44 handleValueCallback C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl. dart 796:13_propagateToListeners C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl. dart 567:5 [_completeWithValue] C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl. dart 640:7回调C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/schedule_microtask. dart 40:11_microtaskLoop C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/schedule_microtask. dart 49:5_startMicrotaskLoop C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_ internal/js_dev_runtime/patch/async_patch. dart 166:十五岁
最后是json(我想获取equipementcontrols列表以在视图中使用):

{
    "id": 6,
    "date": "1976-11-18",
    "nbKm": 9651,
    "fuelLevel": 1,
    "comments": "Similique et velit earum praesentium culpa inventore nostrum. Distinctio quas aliquam nihil qui quis.",
    "vehicle": {
        "id": 1,
        "immatriculationPlate": "HO-152-MG",
        "status": {
            "id": 1,
            "label": "à préparer"
        },
        "model_car": {
            "id": 2,
            "name": "308",
            "nbSeats": 5,
            "brand": "Peugeot",
            "logo": "LOGO_MISSING"
        },
        "bookings": []
    },
    "agent": {
        "id": 2,
        "businessName": "momo",
        "user": {
            "id": 4,
            "name": "agent2",
            "email": "agent2@seven.fr",
            "active": 1,
            "lastLoginDate": "2004-05-30"
        }
    },
    "equipementcontrols": [
        {
            "id": 61,
            "RS": 0,
            "RP": 1,
            "EC": 1,
            "equipement": {
                "id": 1,
                "label": "Aile AV G"
            }
        },
        {
            "id": 62,
            "RS": 0,
            "RP": 0,
            "EC": 0,
            "equipement": {
                "id": 2,
                "label": "Aile AR G"
            }
        },
        {
            "id": 63,
            "RS": 1,
            "RP": 1,
            "EC": 0,
            "equipement": {
                "id": 3,
                "label": "Calandre"
            }
        },
        {
            "id": 64,
            "RS": 1,
            "RP": 1,
            "EC": 0,
            "equipement": {
                "id": 4,
                "label": "Phare AV D"
            }
        },
        {
            "id": 65,
            "RS": 0,
            "RP": 1,
            "EC": 1,
            "equipement": {
                "id": 5,
                "label": "Siège pass"
            }
        },
        {
            "id": 66,
            "RS": 0,
            "RP": 1,
            "EC": 1,
            "equipement": {
                "id": 6,
                "label": "Porte AV G"
            }
        },
        {
            "id": 67,
            "RS": 1,
            "RP": 1,
            "EC": 1,
            "equipement": {
                "id": 7,
                "label": "Aile AV D"
            }
        },
        {
            "id": 68,
            "RS": 0,
            "RP": 1,
            "EC": 0,
            "equipement": {
                "id": 8,
                "label": "Aile AR D"
            }
        },
        {
            "id": 69,
            "RS": 0,
            "RP": 0,
            "EC": 1,
            "equipement": {
                "id": 9,
                "label": "Phare AV G"
            }
        },
        {
            "id": 70,
            "RS": 1,
            "RP": 0,
            "EC": 0,
            "equipement": {
                "id": 10,
                "label": "Siège cond"
            }
        },
        {
            "id": 71,
            "RS": 0,
            "RP": 0,
            "EC": 0,
            "equipement": {
                "id": 11,
                "label": "Tableau de b"
            }
        },
        {
            "id": 72,
            "RS": 0,
            "RP": 0,
            "EC": 1,
            "equipement": {
                "id": 12,
                "label": "Porte AV D"
            }
        }
    ]
}

这是EquipementControl文件:

import 'package:flutter_data/flutter_data.dart';
import 'equipement.dart';

class EquipementControl {
  @override
  final int id;
  final int RS;
  final int RP;
  final int EC;
  final Equipement equipement;

  // Constructor, with syntactic sugar for assignment to members.
  EquipementControl({
    required this.id,
    required this.RS,
    required this.RP,
    required this.EC,
    required this.equipement,
  })
  {
    // Initialization code goes here.

  }
  // Construct an object from a mapped JSON array
  factory EquipementControl.fromJson(Map<String,dynamic> mappedJson){
    return EquipementControl(id: mappedJson['id'] as int,
      RS: mappedJson['RS'],
      RP: mappedJson['RP'],
      EC: mappedJson['EC'],
      equipement: Equipement.fromJson(mappedJson['equipement']),
    );
  }

}

和设备文件:

import 'package:flutter_data/flutter_data.dart';

class Equipement {
  @override
  final int id;
  final String label;

  // Constructor, with syntactic sugar for assignment to members.
  Equipement({
    required this.id,
    required this.label,})
  {
    // Initialization code goes here.

  }
  // Construct an object from a mapped JSON array
  factory Equipement.fromJson(Map<String,dynamic> mappedJson){
    return Equipement(id: mappedJson['id'] as int,
      label: mappedJson['label'],
    );
  }
}

我已经试过删除列表上的,我也试过用一个. castFrom方法来代替. from。但是没有任何效果,如果你有一个想法,我是一个接受者。
提前致谢;)

7qhs6swi

7qhs6swi1#

必须在from方法内部进行Map,而不是外部。

List<EquipementControl>.from(mappedJson['equipementcontrols'].map<EquipementControl>((x) => EquipementControl.fromJson(x as Map<String,dynamic>),),),

相关问题