类型 '(dynamic) => RaceResult' 不是 'transform' Flutter<dynamic, dynamic> 类型 '(String,dynamic) => MapEntry

问题描述 投票:0回答:1

我正面临这个问题,试图用 api 中的数据显示列表。 谁能告诉我哪里错了。

我的视图模型

 Future<Future<List<SprintResults>?>> getSprintResults(String? raceId) async {
    isGridLoading = true;
    sprintResults = null;
    return locator<MotorsportRepository>()
        .getMotorsportSprintByEvent(raceId!)
        .then((sprintResponse) {
      sprintResults = sprintResponse;
      isGridLoading = false;
      notifyListeners();
      return sprintResults;
    }).catchError((onError) {
      log('MotorSportViewModel.getSprintResults $onError');
      isGridLoading = false;
      notifyListeners();
      return null;
    });
  }

json 响应

{
    "data": {
        "results": [
            {
                "driver": {
                    "code": null,
                    "country": "Italy",
                    "countryFlag": "",
                    "firstName": "Francesco",
                    "id": "1064",
                    "lastName": "Bagnaia",
                    "name": "Francesco Bagnaia",
                    "shortName": "F Bagnaia",
                    "sportsPersonId": "60c1fac9-e784-43a8-9c1c-1ac46b7e3af8"
                },
                "laps": "10",
                "pits": "0",
                "points": {
                    "bonus": 0,
                    "penalty": 0,
                    "race": 12,
                    "total": 12
                },
                "pos": "1",
                "status": {
                    "id": 1,
                    "name": "On-Track"
                },
                "teamId": "37",
                "time": "20:35.2700",
                "vehicleNo": "63"
            },
            {
                "driver": {
                    "code": null,
                    "country": "Spain",
                    "countryFlag": "",
                    "firstName": "Alex",
                    "id": "865",
                    "lastName": "Rins",
                    "name": "Alex Rins",
                    "shortName": "A Rins",
                    "sportsPersonId": "16d33b43-aa91-418b-9350-201e86d9a2f9"
                },
                "laps": "10",
                "pits": "0",
                "points": {
                    "bonus": 0,
                    "penalty": 0,
                    "race": 9,
                    "total": 9
                },
                "pos": "2",
                "status": {
                    "id": 1,
                    "name": "On-Track"
                },
                "teamId": "4921",
                "time": "2.545",
                "vehicleNo": "42"
            },
            {
                "driver": {
                    "code": null,
                    "country": "Spain",
                    "countryFlag": "",
                    "firstName": "Jorge",
                    "id": "840",
                    "lastName": "Martin",
                    "name": "Jorge Martin",
                    "shortName": "J Martin",
                    "sportsPersonId": "960aad72-372b-48a9-b272-9d4910b4dabe"
                },
                "laps": "10",
                "pits": "0",
                "points": {
                    "bonus": 0,
                    "penalty": 0,
                    "race": 7,
                    "total": 7
                },
                "pos": "3",
                "status": {
                    "id": 1,
                    "name": "On-Track"
                },
                "teamId": "33",
                "time": "4.706",
                "vehicleNo": "89"
            },
            {
                "driver": {
                    "code": null,
                    "country": "Spain",
                    "countryFlag": "",
                    "firstName": "Aleix",
                    "id": "38",
                    "lastName": "Espargaro",
                    "name": "Aleix Espargaro",
                    "shortName": "A Espargaro",
                    "sportsPersonId": "2567b47c-4740-4652-8e3b-040abd5ded21"
                },
                "laps": "10",
                "pits": "0",
                "points": {
                    "bonus": 0,
                    "penalty": 0,
                    "race": 6,
                    "total": 6
                },
                "pos": "4",
                "status": {
                    "id": 1,
                    "name": "On-Track"
                },
                "teamId": "9511",
                "time": "5.052",
                "vehicleNo": "41"
            },
            {
                "driver": {
                    "code": null,
                    "country": "South Africa",
                    "countryFlag": "",
                    "firstName": "Brad",
                    "id": "35569",
                    "lastName": "Binder",
                    "name": "Brad Binder",
                    "shortName": "B Binder",
                    "sportsPersonId": "fd28cc70-e498-4f29-a232-5f889b58814d"
                },
                "laps": "10",
                "pits": "0",
                "points": {
                    "bonus": 0,
                    "penalty": 0,
                    "race": 5,
                    "total": 5
                },
                "pos": "5",
                "status": {
                    "id": 1,
                    "name": "On-Track"
                },
                "teamId": "6861",
                "time": "8.175",
                "vehicleNo": "33"
            },
            {
                "driver": {
                    "code": "BEZ",
                    "country": "Italy",
                    "countryFlag": "",
                    "firstName": "Marco",
                    "id": "1425",
                    "lastName": "Bezzecchi",
                    "name": "Marco Bezzecchi",
                    "shortName": "M Bezzecchi",
                    "sportsPersonId": "767cd34f-f1dd-4816-8850-619a3e3d0884"
                },
                "laps": "10",
                "pits": "0",
                "points": {
                    "bonus": 0,
                    "penalty": 0,
                    "race": 4,
                    "total": 4
                },
                "pos": "6",
                "status": {
                    "id": 1,
                    "name": "On-Track"
                },
                "teamId": "10541",
                "time": "8.877",
                "vehicleNo": "72"
            },
            {
                "driver": {
                    "code": "MRN",
                    "country": "Italy",
                    "countryFlag": "",
                    "firstName": "Luca",
                    "id": "967",
                    "lastName": "Marini",
                    "name": "Luca Marini",
                    "shortName": "L Marini",
                    "sportsPersonId": "8b5eff20-f181-466c-8e2e-f898cbc927b5"
                },
                "laps": "10",
                "pits": "0",
                "points": {
                    "bonus": 0,
                    "penalty": 0,
                    "race": 3,
                    "total": 3
                },
                "pos": "7",
                "status": {
                    "id": 1,
                    "name": "On-Track"
                },
                "teamId": "10541",
                "time": "9.453",
                "vehicleNo": "10"
            },
            {
                "driver": {
                    "code": null,
                    "country": "Portugal",
                    "countryFlag": "",
                    "firstName": "Miguel",
                    "id": "307",
                    "lastName": "Oliveira",
                    "name": "Miguel Oliveira",
                    "shortName": "M Oliveira",
                    "sportsPersonId": "312262fa-999a-4565-a18f-a6d67e4e6b59"
                },
                "laps": "10",
                "pits": "0",
                "points": {
                    "bonus": 0,
                    "penalty": 0,
                    "race": 2,
                    "total": 2
                },
                "pos": "8",
                "status": {
                    "id": 1,
                    "name": "On-Track"
                },
                "teamId": "10551",
                "time": "10.768",
                "vehicleNo": "88"
            },
            {
                "driver": {
                    "code": null,
                    "country": "Australia",
                    "countryFlag": "",
                    "firstName": "Jack",
                    "id": "486",
                    "lastName": "Miller",
                    "name": "Jack Miller",
                    "shortName": "J Miller",
                    "sportsPersonId": "4690fd92-4274-42b0-baf0-989dc21f5d43"
                },
                "laps": "10",
                "pits": "0",
                "points": {
                    "bonus": 0,
                    "penalty": 0,
                    "race": 1,
                    "total": 1
                },
                "pos": "9",
                "status": {
                    "id": 1,
                    "name": "On-Track"
                },
                "teamId": "6861",
                "time": "12.448",
                "vehicleNo": "43"
            },
            {
                "driver": {
                    "code": null,
                    "country": "Spain",
                    "countryFlag": "",
                    "firstName": "Maverick",
                    "id": "150",
                    "lastName": "Vinales",
                    "name": "Maverick Vinales",
                    "shortName": "M Vinales",
                    "sportsPersonId": "32f9bb23-68d7-4d7a-a978-8ef2a37c9af9"
                },
                "laps": "10",
                "pits": "0",
                "points": {
                    "bonus": 0,
                    "penalty": 0,
                    "race": 0,
                    "total": 0
                },
                "pos": "10",
                "status": {
                    "id": 1,
                    "name": "On-Track"
                },
                "teamId": "9511",
                "time": "12.739",
                "vehicleNo": "12"
            },
            {
                "driver": {
                    "code": null,
                    "country": "France",
                    "countryFlag": "",
                    "firstName": "Johann",
                    "id": "90",
                    "lastName": "Zarco",
                    "name": "Johann Zarco",
                    "shortName": "J Zarco",
                    "sportsPersonId": "2fc6d473-9ec4-426f-a8bc-3cc5858a4173"
                },
                "laps": "10",
                "pits": "0",
                "points": {
                    "bonus": 0,
                    "penalty": 0,
                    "race": 0,
                    "total": 0
                },
                "pos": "11",
                "status": {
                    "id": 1,
                    "name": "On-Track"
                },
                "teamId": "33",
                "time": "14.251",
                "vehicleNo": "5"
            },
            {
                "driver": {
                    "code": "MIR",
                    "country": "Spain",
                    "countryFlag": "",
                    "firstName": "Joan",
                    "id": "77512",
                    "lastName": "Mir",
                    "name": "Joan Mir",
                    "shortName": "J Mir",
                    "sportsPersonId": "e2c49d5f-adae-43aa-b171-801c6cd2315c"
                },
                "laps": "10",
                "pits": "0",
                "points": {
                    "bonus": 0,
                    "penalty": 0,
                    "race": 0,
                    "total": 0
                },
                "pos": "12",
                "status": {
                    "id": 1,
                    "name": "On-Track"
                },
                "teamId": "29",
                "time": "14.988",
                "vehicleNo": "36"
            },
            {
                "driver": {
                    "code": null,
                    "country": "Japan",
                    "countryFlag": "",
                    "firstName": "Takaaki",
                    "id": "8577",
                    "lastName": "Nakagami",
                    "name": "Takaaki Nakagami",
                    "shortName": "T Nakagami",
                    "sportsPersonId": "02dba89e-756f-40a0-80d8-9b4b346607da"
                },
                "laps": "10",
                "pits": "0",
                "points": {
                    "bonus": 0,
                    "penalty": 0,
                    "race": 0,
                    "total": 0
                },
                "pos": "13",
                "status": {
                    "id": 1,
                    "name": "On-Track"
                },
                "teamId": "36",
                "time": "15.592",
                "vehicleNo": "30"
            },
            {
                "driver": {
                    "code": null,
                    "country": "Italy",
                    "countryFlag": "",
                    "firstName": "Franco",
                    "id": "4933",
                    "lastName": "Morbidelli",
                    "name": "Franco Morbidelli",
                    "shortName": "F Morbidelli",
                    "sportsPersonId": "64061f00-bb40-4ec5-a440-1ccef46dd67c"
                },
                "laps": "10",
                "pits": "0",
                "points": {
                    "bonus": 0,
                    "penalty": 0,
                    "race": 0,
                    "total": 0
                },
                "pos": "14",
                "status": {
                    "id": 1,
                    "name": "On-Track"
                },
                "teamId": "9491",
                "time": "16.534",
                "vehicleNo": "21"
            },
            {
                "driver": {
                    "code": "FER",
                    "country": "Spain",
                    "countryFlag": "",
                    "firstName": "Raul",
                    "id": "1427",
                    "lastName": "Fernandez",
                    "name": "Raul Fernandez",
                    "shortName": "R Fernandez",
                    "sportsPersonId": "783bf954-9bd2-42cc-842e-a3a1ec6442d0"
                },
                "laps": "10",
                "pits": "0",
                "points": {
                    "bonus": 0,
                    "penalty": 0,
                    "race": 0,
                    "total": 0
                },
                "pos": "15",
                "status": {
                    "id": 1,
                    "name": "On-Track"
                },
                "teamId": "10551",
                "time": "19.290",
                "vehicleNo": "25"
            },
            {
                "driver": {
                    "code": null,
                    "country": "Spain",
                    "countryFlag": "",
                    "firstName": "Augusto",
                    "id": "89078",
                    "lastName": "Fernandez",
                    "name": "Augusto Fernandez",
                    "shortName": "A Fernandez",
                    "sportsPersonId": "7f106afd-2765-41ae-9611-e2c24c848b0e"
                },
                "laps": "10",
                "pits": "0",
                "points": {
                    "bonus": 0,
                    "penalty": 0,
                    "race": 0,
                    "total": 0
                },
                "pos": "16",
                "status": {
                    "id": 1,
                    "name": "On-Track"
                },
                "teamId": "9201",
                "time": "23.128",
                "vehicleNo": "37"
            },
            {
                "driver": {
                    "code": "GIA",
                    "country": "Italy",
                    "countryFlag": "",
                    "firstName": "Fabio",
                    "id": "3403",
                    "lastName": "Di Giannantonio",
                    "name": "Fabio Di Giannantonio",
                    "shortName": "F Di Giannantonio",
                    "sportsPersonId": "0ca7e1fb-99ae-4881-83b7-84ae2106dcc5"
                },
                "laps": "10",
                "pits": "0",
                "points": {
                    "bonus": 0,
                    "penalty": 0,
                    "race": 0,
                    "total": 0
                },
                "pos": "17",
                "status": {
                    "id": 1,
                    "name": "On-Track"
                },
                "teamId": "10531",
                "time": "25.626",
                "vehicleNo": "49"
            },
            {
                "driver": {
                    "code": "BRA",
                    "country": "Germany",
                    "countryFlag": "/media/g2weh1nr/de.png",
                    "firstName": "Stefan",
                    "id": "250047",
                    "lastName": "Bradl",
                    "name": "Stefan Bradl",
                    "shortName": "S Bradl",
                    "sportsPersonId": "68988b7c-15d5-4683-bcf7-343efa0aa52b"
                },
                "laps": "10",
                "pits": "0",
                "points": {
                    "bonus": 0,
                    "penalty": 0,
                    "race": 0,
                    "total": 0
                },
                "pos": "18",
                "status": {
                    "id": 1,
                    "name": "On-Track"
                },
                "teamId": "29",
                "time": "25.787",
                "vehicleNo": "6"
            },
            {
                "driver": {
                    "code": null,
                    "country": "France",
                    "countryFlag": "",
                    "firstName": "Fabio",
                    "id": "880",
                    "lastName": "Quartararo",
                    "name": "Fabio Quartararo",
                    "shortName": "F Quartararo",
                    "sportsPersonId": "d0c19913-36da-40f2-9d77-a7eab19593af"
                },
                "laps": "10",
                "pits": "0",
                "points": {
                    "bonus": 0,
                    "penalty": 0,
                    "race": 0,
                    "total": 0
                },
                "pos": "19",
                "status": {
                    "id": 1,
                    "name": "On-Track"
                },
                "teamId": "9491",
                "time": "27.169",
                "vehicleNo": "20"
            },
            {
                "driver": {
                    "code": null,
                    "country": "Germany",
                    "countryFlag": "",
                    "firstName": "Jonas",
                    "id": "109",
                    "lastName": "Folger",
                    "name": "Jonas Folger",
                    "shortName": "J Folger",
                    "sportsPersonId": null
                },
                "laps": "10",
                "pits": "0",
                "points": {
                    "bonus": 0,
                    "penalty": 0,
                    "race": 0,
                    "total": 0
                },
                "pos": "20",
                "status": {
                    "id": 1,
                    "name": "On-Track"
                },
                "teamId": "9201",
                "time": "46.973",
                "vehicleNo": "94"
            },
            {
                "driver": {
                    "code": "MAR",
                    "country": "Spain",
                    "countryFlag": "",
                    "firstName": "Alex",
                    "id": "35570",
                    "lastName": "Marquez",
                    "name": "Alex Marquez",
                    "shortName": "A Marquez",
                    "sportsPersonId": "43bcb844-431e-406f-a73b-a730d1e1f12f"
                },
                "laps": "6",
                "pits": "0",
                "points": {
                    "bonus": 0,
                    "penalty": 0,
                    "race": 0,
                    "total": 0
                },
                "pos": "21",
                "status": {
                    "id": 3,
                    "name": "Retired"
                },
                "teamId": "10531",
                "time": "4 LAP(s)",
                "vehicleNo": "73"
            },
            {
                "driver": {
                    "code": null,
                    "country": "Italy",
                    "countryFlag": "",
                    "firstName": "Michele",
                    "id": "257",
                    "lastName": "Pirro",
                    "name": "Michele Pirro",
                    "shortName": "M Pirro",
                    "sportsPersonId": "5491e763-648a-447f-b73a-7ea0876a07d3"
                },
                "laps": "5",
                "pits": "0",
                "points": {
                    "bonus": 0,
                    "penalty": 0,
                    "race": 0,
                    "total": 0
                },
                "pos": "22",
                "status": {
                    "id": 3,
                    "name": "Retired"
                },
                "teamId": "37",
                "time": "5 LAP(s)",
                "vehicleNo": "51"
            }
        ],
        "teams": [
            {
                "engine": "Ducati",
                "id": "37",
                "name": "Ducati  Lenovo Team",
                "shortName": "Ducati  Lenovo Team",
                "sportsTeamId": "7b54beb4-ba12-4da2-b1c1-7e2e202b9802",
                "vehicle": "Ducati Desmosedici GP23",
                "logo": null
            },
            {
                "engine": "Honda",
                "id": "4921",
                "name": "LCR Honda CASTROL",
                "shortName": "LCR Honda CASTROL",
                "sportsTeamId": "aea0a84e-3c8d-444e-9909-2513a467d6d6",
                "vehicle": "Honda RC213V",
                "logo": null
            },
            {
                "engine": "Ducati",
                "id": "33",
                "name": "Pramac Racing",
                "shortName": "Pramac Racing",
                "sportsTeamId": "48193907-d4cb-4d8e-abda-9bd5054387b0",
                "vehicle": "Ducati Desmosedici GP23",
                "logo": null
            },
            {
                "engine": "Aprilia",
                "id": "9511",
                "name": "Aprilia Racing",
                "shortName": "Aprilia Racing",
                "sportsTeamId": "c153902d-86e1-4f6b-9ea6-6559e6e823e9",
                "vehicle": "Aprilia RS-GP",
                "logo": null
            },
            {
                "engine": "KTM",
                "id": "6861",
                "name": "Red Bull KTM Factory Racing",
                "shortName": "Red Bull KTM Factory Racing",
                "sportsTeamId": "c0e6d30e-7bd4-43b8-af3e-a31e8004e4dd",
                "vehicle": "KTM RC16",
                "logo": null
            },
            {
                "engine": "Ducati",
                "id": "10541",
                "name": "Mooney VR46 Racing Team",
                "shortName": "Mooney VR46 Racing Team",
                "sportsTeamId": "3c21d90a-2aaf-4065-8399-f837041ff2c3",
                "vehicle": "Ducati Desmosedici GP22",
                "logo": null
            },
            {
                "engine": "Aprilia",
                "id": "10551",
                "name": "WithU Yamaha RNF MotoGP Racing",
                "shortName": "WithU Yamaha RNF MotoGP Racing",
                "sportsTeamId": "c034d82c-6c5a-40f2-8267-6bb387baf655",
                "vehicle": "RS-GP",
                "logo": null
            },
            {
                "engine": "Honda",
                "id": "29",
                "name": "Repsol Honda Team",
                "shortName": "Repsol Honda Team",
                "sportsTeamId": "9a5ba232-bcb4-4325-8bcb-274a9d2e5390",
                "vehicle": "Honda RC213V",
                "logo": null
            },
            {
                "engine": "Honda",
                "id": "36",
                "name": "LCR Honda",
                "shortName": "LCR Honda",
                "sportsTeamId": "ab3f63c4-e71a-42c3-979e-abb5734f8176",
                "vehicle": "Honda RC213V",
                "logo": null
            },
            {
                "engine": "Yamaha",
                "id": "9491",
                "name": "Monster Energy Yamaha MotoGP",
                "shortName": "Monster Energy Yamaha MotoGP",
                "sportsTeamId": "dd62364d-7fc7-47f7-ac9f-10c2277c382d",
                "vehicle": "Yamaha YZR-M1",
                "logo": null
            },
            {
                "engine": "Gas Gas",
                "id": "9201",
                "name": "Tech3 KTM Factory Racing",
                "shortName": "Tech3 KTM Factory Racing",
                "sportsTeamId": "ef85a5eb-91f1-4846-8694-c262756b043d",
                "vehicle": "RC16",
                "logo": null
            },
            {
                "engine": "Ducati",
                "id": "10531",
                "name": "Gresini Racing MotoGP",
                "shortName": "Gresini Racing MotoGP",
                "sportsTeamId": "61799e9d-6aa2-4eee-8289-6e123bc1ed44",
                "vehicle": "Ducati Desmosedici GP22",
                "logo": null
            }
        ]
    },
    "event": {
        "endDate": "2023-04-15T21:00:00Z",
        "id": "70421",
        "name": "Americas GP",
        "sportsEventId": "4b57b84e-d584-43f5-8a8c-48996e60e1c9",
        "startDate": "2023-04-15T20:00:00Z"
    },
    "grandPreNo": 3,
    "raceTrack": "Circuit of the Americas",
    "season": {
        "id": "2023",
        "name": "2023",
        "sportsSeasonId": "9fa65bc1-ffcd-483b-b69e-976c378bccba"
    },
    "session": {
        "id": 5,
        "name": "Sprint Qualifier"
    },
    "tour": {
        "id": "3",
        "name": "MotoGP",
        "sportsTourId": "f99a9039-aafc-442a-a2c0-0df6d4572c99"
    },
    "venue": {
        "city": "Austin",
        "country": "USA",
        "countryISO3": "USA",
        "name": "Circuit of the Americas, Austin",
        "track": {
            "laps": "20",
            "length": "5.514 KM"
        }
    }
}

我的模特班:

class SprintResults extends Equatable {
  final Data? data;
  final Event? event;
  final int? grandPreNo;
  final String? raceTrack;
  final Season? season;
  final Status? session;
  final Tour? tour;
  final Venue? venue;

  const SprintResults(
      {this.data,
      this.event,
      this.grandPreNo,
      this.raceTrack,
      this.season,
      this.session,
      this.tour,
      this.venue});

  factory SprintResults.fromJson(Map<String, dynamic> json) {
    final data = json['data'] != null ? Data.fromJson(json['data']) : null;
    final event = json['event'] != null ? Event.fromJson(json['event']) : null;
    final grandPreNo = json['grandPreNo'];
    final raceTrack = json['raceTrack'];
    final season =
        json['season'] != null ? Season.fromJson(json['season']) : null;
    final session =
        json['session'] != null ? Status.fromJson(json['session']) : null;
    final tour = json['tour'] != null ? Tour.fromJson(json['tour']) : null;
    final venue = json['venue'] != null ? Venue.fromJson(json['venue']) : null;

    return SprintResults(
        data: data,
        event: event,
        grandPreNo: grandPreNo,
        raceTrack: raceTrack,
        season: season,
        session: session,
        tour: tour,
        venue: venue);
  }

  Map<String, dynamic> toJson() {
    return {
      'data': data,
      'event': event,
      'grandPreNo': grandPreNo,
      'raceTrack': raceTrack,
      'season': season,
      'session': session,
      'tour': tour,
      'venue': venue,
    };
  }

  @override
  List<Object?> get props {
    return [
      data,
      event,
      grandPreNo,
      raceTrack,
      season,
      session,
      tour,
      venue,
    ];
  }
}

从我的 viewmodel 类中,它捕获了 MotorSportViewModel.getSprintResults 类型的错误 '(dynamic) => RaceResult' is not a subtype of type '(String,dynamic) => MapEntry' of 'transform'

您的帮助将不胜感激。

flutter
1个回答
0
投票

致力于此:

// 1 - Here you have to return as I explain by point 2
Future<List<SprintResults>?> getSprintResults(String? raceId) async {
  isGridLoading = true;
  var sprintResults = null;
  try {
    // 2- You have to know what exactly return getMotorsportSprintByEvent then return it as a Future
    sprintResults = await locator<MotorsportRepository>().getMotorsportSprintByEvent(raceId!);
  } catch (e) {
    log('MotorSportViewModel.getSprintResults $e');
  }
  isGridLoading = false;
  notifyListeners();
  return sprintResults;
}
© www.soinside.com 2019 - 2024. All rights reserved.