0

Я не знаю, как декодировать строку в JSON, мне необходимо декодировать объект типа Book и добавить новый json объект в коллекцию. Как мне это лучше реализовать? Строка, где я пытаюсь это сделать:

cart.addToCart(CartElement.fromJson(
   jsonDecode('{"id": ${cart.total+1}, "quantity": 
   ${searchBookInList(cart.items, item)+1}, "book": $item}')
));

Класс Book:


    Book bookFromJson(String str) => Book.fromJson(json.decode(str));

    String bookToJson(Book data) => json.encode(data.toJson());

    class Book {
      Book({
        this.id,
        this.idOfGenre,
        this.isbn,
        this.name,
        this.author,
        this.genre,
        this.year,
        this.quantityOfPages,
        this.publisher,
        this.linkImg,
        this.price,
        this.quantityInStock,
      });

      int id;
      int idOfGenre;
      String isbn;
      String name;
      String author;
      String genre;
      int year;
      int quantityOfPages;
      String publisher;
      String linkImg;
      int price;
      int quantityInStock;

      factory Book.fromJson(Map<String, dynamic> json) => Book(
        id: json["id"],
        idOfGenre: json["idOfGenre"],
        isbn: json["ISBN"],
        name: json["name"],
        author: json["author"],
        year: json["year"],
        quantityOfPages: json["quantityOfPages"],
        publisher: json["publisher"],
        linkImg: json["linkImg"],
        price: json["price"],
        quantityInStock: json["quantityInStock"],
      );

      Map<String, dynamic> toJson() => {
        "id": id,
        "id_of_genre": idOfGenre,
        "ISBN": isbn,
        "name": name,
        "author": author,
        "year": year,
        "quantity_of_pages": quantityOfPages,
        "publisher": publisher,
        "link_IMG": linkImg,
        "price": price,
        "quantity_in_stock": quantityInStock,
      };
    }

Класс CartElement:


    CartElement cartElementFromJson(String str) => CartElement.fromJson(json.decode(str));

    String cartElementToJson(CartElement data) => json.encode(data.toJson());

    class CartElement {
      CartElement({
        this.id,
        this.quantity,
        this.book,
      });

      int id;
      int quantity;
      Book book;

      factory CartElement.fromJson(Map<String, dynamic> json) => CartElement(
        id: json["id"],
        quantity: json["quantity"],
        book: json["Book"],
      );

      Map<String, dynamic> toJson() => {
        "id": id,
        "quantity": quantity,
        "Book": book,
      };
    }

Сама ошибка:

    The following FormatException was thrown while handling a gesture:
    Unexpected character (at character 34)
    {"id": 1, "quantity": 1, "book": Instance of 'Book'}
                                     ^

    When the exception was thrown, this was the stack: 
    #0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1404:5)
    #1      _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1271:9)
    #2      _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:936:22)
    #3      _parseJson (dart:convert-patch/convert_patch.dart:40:10)
    #4      JsonDecoder.convert (dart:convert/json.dart:505:36)
    ...
    Handler: "onLongPress"
    Recognizer: LongPressGestureRecognizer#41046
      debugOwner: GestureDetector
      state: possible

4
  • @АлексейГрадсков как я понял item это Book, тогда будет так: cart.addToCart(CartElement.fromJson(jsonDecode('{"id": ${cart.total+1}, "quantity": ${searchBookInList(cart.items, item)+1}, "book": ${item.toJson()}}')));
    – MiT
    1 дек 2020 в 15:49
  • Но я бы сделал метод copyWith, и избежал бы лишний стерилизации.
    – MiT
    1 дек 2020 в 15:53
  • @MiT да, вы верно подметили, item это Book, после вашего предыдущего ответа с добавлением ${item.toJson()} , я попробовал ваш совет, но он мне не помог и вылезла данная ошибка: Unexpected character (at character 35) {"id": 1, "quantity": 1, "book": {id: 1, id_of_genre: null, ISBN: 43647-n437, ... и указание на место ошибки {id 1 дек 2020 в 16:03
  • json.encode(item.toJson()) или bookToJson(item.toJson()). Обновил ответ.
    – MiT
    1 дек 2020 в 16:10

1 ответ 1

1

Вы передаете в jsonDecode объект, а не Json, toJson() преобразует объект в Json:

cart.addToCart(CartElement.fromJson(
  jsonDecode('{"id": ${cart.total+1}, "quantity": ${searchBookInList(cart.items, item)+1}, "book": bookToJson(item.toJson())}')
));

Второй способ через copyWith, он лучше, тем что нет лишний сериализации:

class CartElement {
  CartElement({
    this.id,
    this.quantity,
    this.book,
  });

  int id;
  int quantity;
  Book book;

  CartElement copyWith({int id, int quantity, Book book}) => CartElement(
        id: id ?? this.id,
        quantity: quantity ?? this.quantity,
        book: book ?? this.book,
      );
...
}

...

CartElement cart = CartElement.fromJson(jsonDecode('{"id": ${cart.total+1}, "quantity": ${searchBookInList(cart.items, item)+1}}'));
cart.addToCart(cart.copyWith(book: item));

factory Book.fromJson(Map<String, dynamic> json) => Book(
    id: json["id"] == null ? null : json["id"],
    idOfGenre: json["id_of_genre"] == null ? null : json["id_of_genre"],
    ...
);

Map<String, dynamic> toJson() => {
    "id": id == null ? null : id,
    "id_of_genre": idOfGenre == null ? null : idOfGenre,
    ...
};
4
  • Я попробовал ваш метод, предложенный в первом варианте, оно сработало, но уже другая ошибка, возможно это в каком то другом классе The getter 'name' was called on null. Receiver: null Tried calling: name , вы не знаете в чем дело? 1 дек 2020 в 16:16
  • У вас name = null. Добавил в ответ пример как вам нужно переделать fromJson и toJson для моделей.
    – MiT
    1 дек 2020 в 16:35
  • спасибо большое 1 дек 2020 в 17:38
  • @АлексейГрадсков если ответ вам подошел, его можно отметить как верный: ✔ (под стрелочками)
    – MiT
    1 дек 2020 в 17:40

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.