Reputation: 174
New to dart just need to convert this model object in Json format with fromJson method. How to send this data by constructor to depict the same thing in dart as by using these model objects coming from firebase. Included full book Model Class into it. Need Help. Thank you.
import 'dart:convert';
import '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'];
}
Upvotes: 0
Views: 492
Reputation: 63559
You can use this model class, If you need to update item use .copyWith
and reassign it
Reading from DocumentSnapshot will be
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,
);
}
}
Upvotes: 1