I’m Calling an api when merchandise is added to cart and if its success the snackbar will present for fulfillment however what if consumer added one after one too many merchandise then the all snackbars will present one after the other for very long time so i need when consumer stops then snackbar ought to present solely as soon as the final one the most recent one with msg
MyCart Api
Timer? debounceSnackBarTimer;
Future addToCart(Bundle package deal) async {
ultimate prefs = await SharedPreferences.getInstance();
ultimate accessToken = prefs.getString('accessToken');
ultimate String apiUrl = await AppNetworkingUrls.addtoCart;
if (accessToken == null) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content material: Textual content(
'Please log in so as to add gadgets to the cart',
model: GoogleFonts.poppins(),
),
),
);
return;
}
ultimate url = Uri.parse(apiUrl);
ultimate headers = {
'Content material-Sort': 'software/json',
'Authorization': 'Bearer $accessToken',
};
ultimate physique = jsonEncode({
'amount': 1,
'productId': product.id,
'packageId': package deal.id,
});
debounceSnackBarTimer?.cancel();
debounceSnackBarTimer = Timer(Period(milliseconds: 800), () async {
strive {
ultimate response = await http.publish(
url,
headers: headers,
physique: physique,
);
if (response.statusCode == 200) {
ultimate responseData = jsonDecode(response.physique);
print(response.physique);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
conduct: SnackBarBehavior.floating,
backgroundColor: AppColors.buttonColor,
length: Period(seconds: 2),
content material: Textual content(
responseData['message'] ?? 'Merchandise added to cart',
model: GoogleFonts.poppins(fontWeight: FontWeight.w500),
),
),
);
ultimate cartProvider = Supplier.of(context, pay attention: false);
await cartProvider.fetchCartItems();
} else {
ultimate responseData = jsonDecode(response.physique);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
conduct: SnackBarBehavior.floating,
backgroundColor: Colours.crimson,
length: Period(seconds: 2),
content material: Textual content(
responseData['message'] ?? 'Failed so as to add merchandise',
model: GoogleFonts.poppins(fontWeight: FontWeight.w500),
),
),
);
}
} catch (error) {
print('Error including to cart: $error');
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
conduct: SnackBarBehavior.floating,
backgroundColor: Colours.crimson,
length: Period(seconds: 2),
content material: Textual content(
'One thing went improper. Please strive once more.',
model: GoogleFonts.poppins(fontWeight: FontWeight.w500),
),
),
);
}
});
}
there’s Diff merchandise and each merchandise have diff packages the consumer will add a number of merchandise additionally however i need to present the most recent as soon as solely
I’ve additionally tried the Debounce however its not working as i needed please if anybody will help me on this debounce is working provided that the consumer provides similar merchandise similar package deal many times then this works however i need it ought to work in all merchandise and packages