代码之家  ›  专栏  ›  技术社区  ›  Roohome Com

工厂未处理的异常:type'(dynamic)=>DataAd'不是type'(String,dynamic)的子类型

  •  0
  • Roohome Com  · 技术社区  · 3 年前

    我一直在想一些问题,但没有像我这样的案例。我从教程中得到了代码,我已经理解了。然而,问题在教程中。它使用静态JSON(列表),而我的使用动态JSON(包含键)。

    这是我的密码

    class CompileAd {
      String status;
      String notification;
      Map<int,DataAd> adData;
    
      CompileAd({
        required this.status,
        required this.notification,
        required this.adData,
      });
    
      factory CompileAd.fromJson(Map<String, dynamic> json) => CompileAd(
            status: json["status"],
            notification: json["notification"],
            adData: Map<int,DataAd>.from(
                json["adData"].map((x) => DataAd.fromJson(x))), => ERROR HERE
          );
    
      Map<String, dynamic> toJson() => {
            "status": status,
            "notification": notification,
            "adData": Map<dynamic,dynamic>.from(adData.map((key,x) => x.toJson())), => HERE ALSO (Actually, I wish to use "jsonEncode(adData)" since its working but no idea which one better)
          };
    }
    

    错误日志

    lib/class/adClass。dart:27:69:错误:类型为“Map”的值<一串 动态>'无法从返回类型为的函数返回 “地图输入<动态的,动态的。

    • “地图”来自“飞镖:核心”。
    • “MapEntry”来自“dart:core”。 “adData”:地图<动态,动态>。from(adData.map)((key,x)=>x、 toJson()), ^

    下面是json对象

    adData=>(keyID,adObject)

    {
        "adData": {
            "77289": {
                "id": 77289,
                "username": "magdatjahja@gmail.com",
                "category": "rumah",
                "subCategory": "rumah",
                "type": "dijual",
                "uniqueId": "6436",
                "title": "Jual Rumah di Mazenta Bintaro Tangerang Selatan",
                "slug": "jual-rumah-di-mazenta-bintaro-tangerang-selatan",
                "content": "Jual rumah di mazento bintaro tangerang selatan\r\n\r\nSatu-satunya hunian premium di kawasan Bintaro bernuansa Japan Ambiance Living :\r\n\r\nType unit :\r\n\ud83d\udc49 Type 6 \r\nLuas Tanah: 72m2 \r\nLuas Bangunan: 83m2 \r\nKamar Tidur: 3 \r\nKamar Mandi: 2 \r\n\r\n\ud83c\udf81Dapatkan Promo Early Bird Price !!!\ud83c\udf81\r\n\u2705HARGA MULAI RP.1,8 M\r\n\u27053 unit AC 1 Pk\r\n\u2705Free Mini Canopy\r\n\u2705 Lucky Bowl up to Iphone13\r\n\u2705 SmartDoorlock\r\n\r\nLokasi\r\n\ud83d\ude97 Hanya 3 menit ke BXchange Mall Bintaro!\r\n\ud83d\ude97 Hanya 2-3 menit ke pintu tol! dan jumlah unit SANGAT terbatas!\r\n\ud83d\ude97Hanya 3 Menit Statiun Jurang Mangu\r\n\ud83d\ude97Hanya 3 menit UPJ University\r\n\r\n\r\nHubungi AGENT MERKETING\r\nMagda 08118897878",
                "bid": 2000,
                "balance": 1,
                "price": "1800000000",
                "province": "banten",
                "regency": "tangerang selatan",
                "district": "",
                "village": "",
                "complex": "",
                "rd": 1,
                "premier": "2022-03-22 11:33:13",
                "partner": 1,
                "privateSync": "0000-00-00 00:00:00",
                "penaltyTime": null,
                "data": {
                    "price": {
                        "price": "1800000000"
                    },
                    "priceUnit": "",
                    "province": "banten",
                    "regency": "tangerang selatan",
                    "district": "",
                    "village": "",
                    "additional": {
                        "address": "Bintaro, Tangerang Selatan",
                        "complex": "",
                        "lt": "72",
                        "lb": "83",
                        "bedroom": "3",
                        "bathroom": "2",
                        "maidbedroom": "",
                        "maidbathroom": "",
                        "floor": "",
                        "garage": "",
                        "carports": "",
                        "electricity": "",
                        "orientation": "",
                        "interior": ""
                    },
                    "facebook": "2022-03-22 14:54:05",
                    "twitter": "0000-00-00 00:00:00",
                    "pinterest": "0000-00-00 00:00:00",
                    "tumblr": "0000-00-00 00:00:00",
                    "premier": "2022-03-22 11:33:13",
                    "partner": true,
                    "penaltyTime": "2022-03-22 11:32:01",
                    "instagram": "2022-03-22 14:47:14"
                },
                "images": {
                    "images": ["jual-rumah-di-mazenta-bintaro-tangerang-selatan-0XOV3B.jpg", "jual-rumah-di-mazenta-bintaro-tangerang-selatan-VS38QN.jpg", "jual-rumah-di-mazenta-bintaro-tangerang-selatan-14RHI3.jpg"],
                    "imagesAdditional": [],
                    "images360": []
                },
                "video": {
                    "1": "https:\/\/youtu.be\/CA6iNWEDa1E"
                },
                "sosmed": 0,
                "view": 28,
                "contact": 0,
                "status": "active",
                "checked": 1,
                "sold": "0000-00-00 00:00:00",
                "modify": "0000-00-00 00:00:00",
                "waktu": "2022-03-21 17:40:03",
                "dir": "\/uploads\/images\/2022\/03\/77289\/",
                "url": "\/properti\/tangerang-selatan\/6436-jual-rumah-di-mazenta-bintaro-tangerang-selatan\/"
            }
        },
        "status": "success",
        "notification": "Berhasil memproses permintaan"
    }
    

    我真的很感激你的回答。非常感谢。

    0 回复  |  直到 3 年前