Я пытаюсь сопоставить firebase DocumentSnapshot с пользовательским классом 'Comp', показанным ниже. Если я создаю пустой документ в firebase и пытаюсь сопоставить его с классом comp, я получаю нулевой экземпляр этого класса.
Я попытался позаботиться о возможности нулевых значений из Firebase, используя ?? и троичные операторы.
Важной информацией является то, что эта проблема решается сама собой, если я удаляю imageCaroselList из класса comp.
import 'package:cloud_firestore/cloud_firestore.dart';
class Comp {
final String id;
final String compName;
final String locationCityState;
final String hostSiteGymName;
final String hostSiteAddress;
final String contactEmail;
final String contactPhone;
final String featureImageURL;
final String date;
final String infoText;
final List<String> imageCaroselList;
Comp({
this.id,
this.compName,
this.locationCityState,
this.hostSiteGymName,
this.hostSiteAddress,
this.contactEmail,
this.contactPhone,
this.featureImageURL,
this.date,
this.infoText,
this.imageCaroselList
});
factory Comp.fromFirestore(DocumentSnapshot doc) {
Map data = doc.data;
return Comp(
id: doc.documentID,
compName: data['Comp Name'] ?? 'Comp Name',
locationCityState: data['Location City, State'] ?? 'Location City, State',
hostSiteGymName: data['Host Site/Gym Name'] ?? 'Host Site/Gym Name',
hostSiteAddress: data['Host Site/Gym Address'] ?? 'Host Site/Gym Address',
contactEmail: data['Contact Email'] ?? 'Contact Email',
contactPhone: data['Contact Phone'] ?? 'Contact Phone',
featureImageURL: data['Feature Image URL'] ?? 'Feature Image URL',
date: data['Date'] ?? 'Date',
infoText: data['Main Info Text'] ?? 'No info Text Available',
imageCaroselList: List.from(data['Image Carosel URL List']).isEmpty || List.from(data['Image Carosel URL List']) == null ? []: List.from(data['Image Carosel URL List']) ,
);
}
}
Приведенный ниже код отображает как ноль:
class _UpcomingCompsTabState extends State<UpcomingCompsTab> {
final DatabaseService _db = DatabaseService();
@override
Widget build(BuildContext context) {
//subscribing to a stream of comp items
return StreamBuilder<List<Comp>>(
stream: _db.comps,
builder: (context, snapshot) {
//converting the snapshot of list to an actual list
List<Comp> compList = snapshot.data;
if (compList != null) {
Также стоит отметить, что если документ завершен, а не пуст, в Firebase этот код работает просто отлично