I have a button in my app, whenever a user clicks on the button in a day, he won’t be able to click on the button again for the whole day, and when he clicks on the button, the button will be inactive all throughout the day and will be active tomorrow. i saved the time and date of users last clicked on firestore but my issues here is when the button is inactive, it also execute, please below is my code, what am i getting wrong here. i will really appreciate any help
@override
void initState() {
super.initState();
_checkButtonStatus();
}
verifyAccount() async {
final response = await http.post(
Uri.parse('http://'),
headers: {
"Content-type": "application/x-www-form-urlencoded",
"Accept": "application/json",
},
body:{
"accountNo": widget.valueAccountNo,
"accountName": widget.valueAccountName,
"bankName": widget.valueBankName,
});
var data = await json.decode(json.encode(response.body));
if(data == "Error"){
}
else{
//Get Your ERROR message's here
}
}
Future<void> _checkButtonStatus() async {
User? user = _auth.currentUser;
_uid = user!.uid;
try {
DocumentSnapshot documentSnapshot =
await _firestore.collection('Withdrawals').doc(_uid).get();
if (documentSnapshot.exists) {
Timestamp lastPressed = documentSnapshot['date'];
DateTime lastPressedDate = lastPressed.toDate();
DateTime currentDate = DateTime.now();
Duration difference = currentDate.difference(lastPressedDate);
setState(() {
_isButtonActive = difference.inHours >= 24;
});
}
} catch (e) {
print("Error checking button status: $e");
}
}
Future<void> _handleButtonPress() async {
try {
await _firestore.collection('Withdrawals').doc(_uid).set({
'date': Timestamp.now(),
});
setState(() {
_isButtonActive = false;
});
} catch (e) {
print("Error handling button press: $e");
}
}
child: ElevatedButton(
child: Text(_isButtonActive ? 'Continue':'Disabled'),
onPressed: _isButtonActive ? () async {
await _handleButtonPress();
verifyAccount();
} : null,
),
It shouldn't verify or go to verifyAccount() method if the button is inactive, please is there anything am missing or am not doing well please
2
Answers
My suggestion was to store the state of the button as a
Future<bool>
and refactor your function_checkButtonStatus()
into_getButtonStatus()
a function that returns a future value.This also removes the problem of calling
setState
frominitState
.The example below is adapted from the solution shown at: FutureBuilder. It can be copy/pasted and run in a dartpad.
do it like that
instead of passing null pass an empty function.