The following NoSuchMethodError was thrown building FutureBuilder<DocumentSnapshot<Map<String, dynamic>>>(dirty, state: _FutureBuilderState<DocumentSnapshot<Map<String, dynamic>>>#6c166):
The getter 'hasData' was called on null.
Receiver: null
Tried calling: hasData
I get this issue when like my product and this product added to favorite screen.can anyone know what is the error.all packages is up to date with null safety
class _FavoriteScreenState extends State<FavoriteScreen> {
@override
Widget build(BuildContext context) {
return Container(
child: StreamBuilder(
stream: FirebaseFirestore.instance
.collection('users')
.doc(parentIdGlobal)
.collection('favorites')
.snapshots(),
builder: (context, AsyncSnapshot snapshot) {
if (!snapshot.hasData)
return Center(
child: CircularProgressIndicator(),
);
final List<QueryDocumentSnapshot<Map<String, dynamic>>> docSnapList =
snapshot.data?.docs ?? [];
if (docSnapList.isEmpty) {
return Center(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: WebsafeSvg.asset(
'assets/images/no_favorite_item.svg',
fit: BoxFit.cover,
height: MediaQuery.of(context).size.height / 5,
),
),
);
}
final List<Map<String, dynamic>> docList = docSnapList.map((QueryDocumentSnapshot<Map<String, dynamic>> queryDocumentSnapshot) => queryDocumentSnapshot.data()).toList();
return ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: docList.length,
itemBuilder: (context, index) {
if (docSnapList.isEmpty) {
return Center(child: Text('No items yet!'));
}
final itemId = docList[index]['id'];
final isLiked = docList[index]['isLiked'];
return Visibility(
visible: isLiked,
child: FutureBuilder(
future: FirebaseFirestore.instance
.collection('items')
.doc(itemId)
.get(),
builder: (context, AsyncSnapshot) {
var data;
if (!data.hasData)
return Center(
child: CircularProgressIndicator(),
);
bool isSold = docList[index]['isSold'];
bool isLiked = docList[index]['isLiked'];
String itemId = docList[index]['itemId'];
String seller = docList[index]['seller'];
String sellerName = docList[index]['sellerName'];
String title = docList[index]['title'];
String desc = docList[index]['desc'];
String price = docList[index]['price'];
String condition = docList[index]['condition'];
String category = docList[index]['category'];
String location = docList[index]['location'];
String itemImage = docList[index]['imageDownloadUrl'];
print('ITEM IMAGE VALUE: $itemImage');
return EcommerceScreenProductItem(
itemId: itemId,
seller: seller,
sellerName: sellerName,
title: title,
desc: desc,
price: price,
isLiked: isLiked,
isSold: isSold,
condition: condition,
category: category,
location: location,
itemImage: itemImage,
);
},
),
);
});
},
),
);
}
}
CodePudding user response:
Try changing
if (!snapshot.hasData)
return Center(
child: CircularProgressIndicator(),
);
to
if (snapshot==null || !snapshot.hasData)
return Center(
child: CircularProgressIndicator(),
);
CodePudding user response:
Try to change this line
if (!data.hasData)
by this line
if (!AsyncSnapshot.hasData)