将对象转换为可编码对象失败。扑

时间:2020-04-15 11:56:18

标签: flutter dart

我有那些课:

预订类似乎是导致问题的原因,但我无法弄清原因。

class Reservation {
  final String id;
  final List<Seat> seat;
  final Customer customer;
  final Performance performance;

  Reservation({
    @required this.id,
    @required this.seat,
    @required this.customer,
    @required this.performance,
  });

  factory Reservation.fromJson(Map<String, dynamic> json) {
    var list = json['seats'] as List;
    List<Seat> seatList = list.map((s) => Seat.fromJson(s)).toList();
    return Reservation(
      id: json['id'],
      seat: seatList,
      customer: Customer.fromJson(json['customer']),
      performance: Performance.fromJson(json['performance']),
    );
  }

  Map<String, dynamic> toJson() => {
        'id': id,
        'seat': seat,
        'customer': customer,
        'performance': performance,
      };
}
class Seat {
  final String seatId;
  final int seatNumber;
  bool isReserved;

  Seat(
      {@required this.seatId,
      @required this.isReserved,
      @required this.seatNumber});

  factory Seat.fromJson(Map<String, dynamic> json) {
    return Seat(
      seatId: json['seatId'],
      isReserved: json['isReserved'],
      seatNumber: json['seatNumber'],
    );
  }

  Map<String, dynamic> toJson() => {
        'seatId': seatId,
        'seatNumber': seatNumber,
        'isReserved': isReserved,
      };
}
class Customer {
  final String id;
  final String firstName;
  final String lastName;
  final String phoneNumber;

  Customer({
    @required this.id,
    @required this.firstName,
    @required this.lastName,
    @required this.phoneNumber,
  });

  factory Customer.fromJson(Map<String, dynamic> json) {
    return Customer(
      id: json['id'],
      firstName: json['firstName'],
      lastName: json['lastName'],
      phoneNumber: json['phoneNumber'],
    );
  }

  Map<String, dynamic> toJson() => {
        'id': id,
        'firstName': firstName,
        'lastName': lastName,
        'phoneNumber': phoneNumber,
      };
}

class Movie {
  final String id;
  final String title;
  final int year;
  final int duration;
  final int minAge;
  final String description;
  final String language;
  final String imageUrl;

  const Movie({
    @required this.id,
    @required this.title,
    @required this.year,
    @required this.duration,
    @required this.minAge,
    @required this.description,
    @required this.language,
    @required this.imageUrl,
  });

  factory Movie.fromJson(Map<String, dynamic> json) {
    return Movie(
      id: json['id'],
      title: json['title'],
      year: json['year'],
      duration: json['duration'],
      minAge: json['minAge'],
      description: json['description'],
      language: json['language'],
      imageUrl: json['imageUrl'],
    );
  }

  Map<String, dynamic> toJson() => {
        'id': id,
        'title': title,
        'year': year,
        'duration': duration,
        'minAge': minAge,
        'description': description,
        'language': language,
        'imageUrl': imageUrl,
      };
}

class Performance {
  final String id;
  final Theater theater;
  final Movie movie;
  final DateTime dateTime;
  final List<Reservation> reservations;

  Performance({
    @required this.id,
    @required this.movie,
    @required this.theater,
    @required this.dateTime,
    this.reservations,
  });

  factory Performance.fromJson(Map<String, dynamic> json) {
    return Performance(
      id: json['id'],
      movie: Movie.fromJson(json['movie']),
      theater: Theater.fromJson(json['theater']),
      dateTime: json['dateTime'],
    );
  }

  Map<String, dynamic> toJson() => {
        'id': id,
        'movie': movie,
        'theater': theater,
        'dateTime': dateTime,
      };
}

class Theater {
  final String id;
  final String theaterName;
  final int seatsPerRow;
  final int rows;
  final List<Seat> seats;

  Theater({
    @required this.id,
    @required this.theaterName,
    @required this.seatsPerRow,
    @required this.rows,
    @required this.seats,
  });

  factory Theater.fromJson(Map<String, dynamic> json) {
    var seat = json['seats'] as List;
    List<Seat> seats = seat.map((i) => Seat.fromJson(i)).toList();
    return Theater(
      id: json['id'],
      theaterName: json['theaterName'],
      seatsPerRow: json['seatsPerRow'],
      rows: json['rows'],
      seats: seats,
    );
  }

  Map<String, dynamic> toJson() => {
        'id': id,
        'theaterName': theaterName,
        'seatsPerRow': seatsPerRow,
        'rows': rows,
        'seats': seats
      };
}

因此,我尝试使用Item.fromJson()方法以及toJson方法处理转换。 但是,无论何时我尝试使用以下代码添加新的预订:

Future<void> addReservation(Reservation reservation) async {
    final url = 'http://example.com';
    try {
      final response = await http.post(
        url,
        body: json.encode({
          'seatNumber': reservation.seat,
          'customer': reservation.customer,
          'performance': reservation.performance,
        }),
      );

      final newReservation = Reservation(
        id: json.decode(response.body)['name'],
        seat: reservation.seat,
        customer: reservation.customer,
        performance: reservation.performance,
      );
      _reservation.add(newReservation);
      notifyListeners();
    } catch (err) {
      print(err);
    }
  }

void _saveReservation() {
    _newReservation = Reservation(
      id: _newReservation.id,
      seat: _newReservation.seat,
      customer: _newReservation.customer,
      performance: _newReservation.performance,
    );
    Provider.of<ReservationProvider>(context, listen: false)
        .addReservation(_newReservation);
    Navigator.pop(context);
  }

我收到“将对象转换为可编码对象失败:“性能”实例的错误”。

0 个答案:

没有答案