需要在Dart中从Json格式转换此模型对象



dart的新手只需要使用fromJson方法将该模型对象转换为Json格式。如何发送这些数据由构造函数在dart中描述,就像使用这些来自firebase的模型对象一样。包括完整的书籍模型类。需要帮助。非常感谢。

导入"dart:convert";

导入'package: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'];
}

您可以使用此模型类,如果您需要更新项目,请使用.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,
);
}
}

最新更新