繁体   English   中英

需要将此 model object 从 Json 格式转换为 Z35900D987289A83AF101D18A9F7C

[英]need to convert this model object from Json format in Dart

dart 的新手只需使用 from 方法将 model object 转换为 ZEED8D85B888A6C01333424J8ZEE6 格式。 如何通过构造函数发送此数据以在 dart 中描述相同的内容,就像使用来自 firebase 的这些 model 对象一样。 包含整本书 Model Class 到其中。 需要帮忙。 谢谢你。

导入“飞镖:转换”;

导入“包:get/get.dart”;

class Book {
  Book({
    required this.author,
    required this.bookID,
    required this.discountPercentage,
    required this.description,
    required this.title,
    required this.itemCount,
    required this.price,
    this.isLiked = false,
    required this.ratings,
    required this.coverImage,
  });
  late String author;
  late String bookID;
  late int discountPercentage;
  late String description;
  late String title;
  late double price;
  late double ratings;
  late bool isLiked;
  var itemCount = 0.obs;
  late String coverImage;
  bool? isRecommended = false;
  Book.fromJson(Map<String, dynamic> json, this.bookID) {
    author = json['author'];
    bookID = json['bookID'];
    discountPercentage = json['discountPercentage'];
    itemCount = json['itemCount'];
    description = json['description'];
    title = json['title'];
    price = json['price'];
    isLiked = false;
    ratings = json['ratings'];
    coverImage = json['coverImage'];
    isRecommended = json['isRecommended'];
  }
  Book.fromSnapShot(var firestoreSnapshot) {
    author = firestoreSnapshot['author'];
    bookID = firestoreSnapshot['bookID'];
    discountPercentage = firestoreSnapshot['discountPercentage'];
    itemCount = firestoreSnapshot['itemCount'];
    description = firestoreSnapshot['description'];
    title = firestoreSnapshot['title'];
    price = firestoreSnapshot['price'];
    isLiked = false;
    ratings = firestoreSnapshot['ratings'];
    coverImage = firestoreSnapshot['coverImage'];
  }

  Map<String, dynamic> toJson() {
    final _data = <String, dynamic>{};
    _data['author'] = author;
    _data['bookID'] = bookID;
    _data['discountPercentage'] = discountPercentage;
    _data['description'] = description;
    _data['title'] = title;
    _data['price'] = price;
    _data['coverImage'] = coverImage;
    _data['itemCount'] = itemCount;
    return _data;
  }

  setItemcount(int count) {
    itemCount.value = count;
  }
}



Future getBooks() async {
    RxInt itemCount = 1.obs;
    QuerySnapshot querySnapshot =
        await FirebaseFirestore.instance.collection("books").get();

    var bookList = [];
    for (var doc in querySnapshot.docs) {
      Book book = Book(
        author: doc['author'],
        title: doc['title'],
        price: doc['price'].toDouble(),
        coverImage: doc['coverImage'],
        bookID: doc.id,
        ratings: doc['ratings'].toDouble(),
        description: doc['description'],
        discountPercentage: doc['discountPercentage'],
        isLiked: false,
        itemCount: itemCount,
      );
      bookList.add(book);
    }
    return bookList;
  }



Book.fromJson(Map<String, dynamic> json, this.bookID) {
    author = json['author'];
    bookID = json['bookID'];
    discountPercentage = json['discountPercentage'];
    itemCount = json['itemCount'];
    description = json['description'];
    title = json['title'];
    price = json['price'];
    isLiked = false;
    ratings = json['ratings'];
    coverImage = json['coverImage'];
    isRecommended = json['isRecommended'];
  }

您可以使用此 model class,如果您需要更新项目使用.copyWith并重新分配它

从 DocumentSnapshot 读取将是

factory Book.fromFirestore(
    DocumentSnapshot<Map<String, dynamic>> snapshot,
    SnapshotOptions? options,
  ) {
    final map = snapshot.data();
    return Book(
      author: map?['author'] ?? '',
      bookID: map?['bookID'] ?? '',
      discountPercentage: map?['discountPercentage']?.toInt() ?? 0,
      description: map?['description'] ?? '',
      title: map?['title'] ?? '',
      price: double.tryParse(map?['price']) ?? 0.0,
      ratings: double.tryParse(map?['ratings']) ?? 0.0,
      isLiked: map?['isLiked'] ?? false,
      itemCount: int.tryParse(map?['itemCount']) ?? 0,
      coverImage: map?['coverImage'] ?? '',
      isRecommended: map?['isRecommended'],
    );
  }
import 'dart:convert';

import 'package:cloud_firestore/cloud_firestore.dart';

class Book {
  final String author;
  final String bookID;
  final int discountPercentage;
  final String description;
  final String title;
  final double price;
  final double ratings;
  final bool isLiked;
  final int itemCount;
  final String coverImage;
  bool? isRecommended;
  Book({
    required this.author,
    required this.bookID,
    required this.discountPercentage,
    required this.description,
    required this.title,
    required this.price,
    required this.ratings,
    required this.isLiked,
    required this.coverImage,
    this.itemCount = 0,
    this.isRecommended = false,
  });

  Map<String, dynamic> toMap() {
    final result = <String, dynamic>{};

    result.addAll({'author': author});
    result.addAll({'bookID': bookID});
    result.addAll({'discountPercentage': discountPercentage});
    result.addAll({'description': description});
    result.addAll({'title': title});
    result.addAll({'price': price});
    result.addAll({'ratings': ratings});
    result.addAll({'isLiked': isLiked});
    result.addAll({'itemCount': itemCount});
    result.addAll({'coverImage': coverImage});
    if (isRecommended != null) {
      result.addAll({'isRecommended': isRecommended});
    }

    return result;
  }

  factory Book.fromFirestore(
    DocumentSnapshot<Map<String, dynamic>> snapshot,
    SnapshotOptions? options,
  ) {
    final map = snapshot.data();
    return Book(
      author: map?['author'] ?? '',
      bookID: map?['bookID'] ?? '',
      discountPercentage: map?['discountPercentage']?.toInt() ?? 0,
      description: map?['description'] ?? '',
      title: map?['title'] ?? '',
      price: double.tryParse(map?['price']) ?? 0.0,
      ratings: double.tryParse(map?['ratings']) ?? 0.0,
      isLiked: map?['isLiked'] ?? false,
      itemCount: int.tryParse(map?['itemCount']) ?? 0,
      coverImage: map?['coverImage'] ?? '',
      isRecommended: map?['isRecommended'],
    );
  }

  String toJson() => json.encode(toMap());

  Book copyWith({
    String? author,
    String? bookID,
    int? discountPercentage,
    String? description,
    String? title,
    double? price,
    double? ratings,
    bool? isLiked,
    int? itemCount,
    String? coverImage,
    bool? isRecommended,
  }) {
    return Book(
      author: author ?? this.author,
      bookID: bookID ?? this.bookID,
      discountPercentage: discountPercentage ?? this.discountPercentage,
      description: description ?? this.description,
      title: title ?? this.title,
      price: price ?? this.price,
      ratings: ratings ?? this.ratings,
      isLiked: isLiked ?? this.isLiked,
      itemCount: itemCount ?? this.itemCount,
      coverImage: coverImage ?? this.coverImage,
      isRecommended: isRecommended ?? this.isRecommended,
    );
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM