Reputation: 1
I have problem in model class for fetching data from real-time firebase database into list but receiving null. Further clarify by images below:
This is image that data to fetch.
This is a model class:
class Seller {
var key;
String address,
businessType,
description,
fcm,
joinTimeStamp,
name,
onlineStatus,
picUrl,
uuid;
int blockByAdmin, completeOrders, rating;
double lat;
double lng;
Seller(
this.address,
this.blockByAdmin,
this.businessType,
this.description,
this.completeOrders,
this.fcm,
this.joinTimeStamp,
this.lat,
this.lng,
this.name,
this.onlineStatus,
this.picUrl,
this.rating,
this.uuid);
Seller.fromSnapshot(DataSnapshot snapshot)
: key = snapshot.value,
address = snapshot.value["address"],
blockByAdmin = snapshot.value["blockByAdmin"],
businessType = snapshot.value["businessType"],
description = snapshot.value["description"],
completeOrders = snapshot.value["completeOrders"],
fcm = snapshot.value["fcm"],
joinTimeStamp = snapshot.value["joinTimeStamp"],
lat = snapshot.value["lat"],
lng = snapshot.value["lng"],
name = snapshot.value["name"],
onlineStatus = snapshot.value["onlineStatus"],
picUrl = snapshot.value["picUrl"],
rating = snapshot.value["rating"],
uuid = snapshot.value["uuid"];
toJson() {
return {
'address': address,
'blockByAdmin': blockByAdmin,
'businessType': businessType,
'description': description,
'completeOrders': completeOrders,
'fcm': fcm,
'joinTimeStamp': joinTimeStamp,
'lat': lat,
'lng': lng,
'name': name,
'onlineStatus': onlineStatus,
'picUrl': picUrl,
'rating': rating,
'uuid': uuid,
};
}
}
This is a class where we are using model class and getting null at list length:
class Body extends StatefulWidget {
@override
_BodyState createState() => _BodyState();
}
class _BodyState extends State<Body> {
final urlImage =
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSPlqk-tNmFTjT7q_edAjaYLU5qf2cFUM9vfrweUbqnS_58LqF7AMp67KVdslIubuzy9b4&usqp=CAU';
final _database = FirebaseDatabase.instance.reference().child('kjobhee');
List<Seller> _sellerList;
StreamSubscription<Event> _onTodoAddedSubscription;
Query _query;
@override
void initState() {
super.initState();
_activateListeners();
}
void _activateListeners() {
_query = _database.child('seller');
_onTodoAddedSubscription = _query.onValue.listen(onEntryAdded);
}
onEntryAdded(Event event) {
setState(() {
_sellerList.add(Seller.fromSnapshot(event.snapshot));
print(_sellerList);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: kPrimaryColor,
),
drawer: NavigationDrawer(),
body: RefreshIndicator(
onRefresh: () => _buildBuyerProfile(),
child: _sellerList.length > 0
? ListView.builder(
shrinkWrap: true,
itemCount: _sellerList.length,
itemBuilder: (BuildContext context, int index) {
String key = _sellerList[index].key;
String address = _sellerList[index].address;
int blockByAdmin = _sellerList[index].blockByAdmin;
String businessType = _sellerList[index].businessType;
String description = _sellerList[index].description;
int completeOrders = _sellerList[index].completeOrders;
String fcm = _sellerList[index].fcm;
String joinTimeStamp = _sellerList[index].joinTimeStamp;
double lat = _sellerList[index].lat;
double lng = _sellerList[index].lng;
String name = _sellerList[index].name;
String onlineStatus = _sellerList[index].onlineStatus;
String picUrl = _sellerList[index].picUrl;
int rating = _sellerList[index].rating;
String uuid = _sellerList[index].uuid;
print(key +
'/' +
address +
'/' +
blockByAdmin.toString() +
'/' +
businessType +
'/' +
description +
'/' +
completeOrders.toString() +
'/' +
fcm +
'/' +
joinTimeStamp +
'/' +
lat.toString() +
'/' +
lng.toString() +
'/' +
name +
'/' +
onlineStatus +
'/' +
picUrl +
'/' +
rating.toString() +
'/' +
uuid);
return _buildBuyerProfile();
})
: Center(
child: CircularProgressIndicator(),
),
),
);
}
I would be very grateful for any user help.
Upvotes: 0
Views: 661
Reputation: 263
I think you need to handle null in your model
like this -
Seller.fromSnapshot(DataSnapshot snapshot)
: key = snapshot.value,
address = snapshot.value["address"] ?? '',
blockByAdmin = snapshot.value["blockByAdmin"] ?? '',
businessType = snapshot.value["businessType"] ?? '',
description = snapshot.value["description"] ?? '',
completeOrders = snapshot.value["completeOrders"] ?? '',
fcm = snapshot.value["fcm"] ?? '',
joinTimeStamp = snapshot.value["joinTimeStamp"] ?? '',
lat = snapshot.value["lat"] ?? '',
lng = snapshot.value["lng"] ?? '',
name = snapshot.value["name"] ?? '',
onlineStatus = snapshot.value["onlineStatus"] ?? '',
picUrl = snapshot.value["picUrl"] ?? '',
rating = snapshot.value["rating"] ?? '',
uuid = snapshot.value["uuid"] ?? '';
maybe your problem will be solved. i did't try that but you can!
Upvotes: 0
Reputation: 21
You need to consider few things here:
You need to make sure that you are getting the data from Firebase, try to print it in log and check if you are receiving the data.
You need to mange your UI, considering that when the screen load first your data will not be there that means your variable _sellerList will be null. So need to check that in your code which is missing.
If you are not getting the data from firebase but but I am considering that the configuration is done properly then may be you can you can use the same code to fetch that than I am using:
Future<void> fetchProducts() async {
await FirebaseFirestore.instance
.collection(FirebaseCollectionConst.productsCollection)
.get()
.then((QuerySnapshot productsData) {
_products = [];
productsData.docs.forEach((element) {
_products.insert(0, Product(
id: element.get('productId'),
title: element.get('productTitle'),
description: element.get('productDescription'),
price: double.parse(element.get('price')),
imageUrl: element.get('productImage'),
brand: element.get('productBrand'),
productCategoryName: element.get('productCategory'),
quantity: int.parse(element.get('productQuality')),
isFavourite: false,
isPopular: true));
});
});
}
Here I am fetch a collection of products and collecting into a class member which extends ChangeNotifier class and showing that data with the help of Provider.
Upvotes: 2