I have a Bool variable which, I set to true when any API in my flutter app throw an exception, so whenever the Bool variable is true ,I want to display an error message, as the API call is asynchronous so I want to keep on checking that the Bool variable is true or false, so how can I keep on checking or whenever the variable changes how can I execute some lines of code after the Bool is true in flutter,
Asked
Active
Viewed 234 times
-1
-
1Please add executable sources. – Jungwon Aug 24 '22 at 08:57
-
1Brother you better start learning state management approaches. – HKN Aug 24 '22 at 08:58
-
how can i trigger a task whenever a bool is changed – ASISH KUMAR Aug 24 '22 at 09:11
-
You don't need the bool to make it happen. Creat a funtion with the code you want to run and call it the then() method of your async function or the stop the flow complety using await. Your use of the bool is misleading, you most likely don't need it. Regarding UI you use a statemanagment to have your logic variavels organized and able to trigger/show changes on the screen. I posted an awnser here to help you out. – MarcoFerreira Aug 24 '22 at 09:26
1 Answers
0
It seems you are really just starting with coding or Flutter. You also haven't provide any code.
Based only in your current question I will point 2 aspects to help you:
- Regarding logic/Function: You don't need to check a bool. In the assync Function you can check the result and execute a function acoordingly to that. You may use
await
outhen
as you wish. - Regarding UI: if you need the UI to update based on the work of that function or variable change you will have to use a state managment solution. You have plenty and you should take a look. I personally use mobx for its simplicity and powerfull use cases, but there are manny other and this is a personal and project choice.
You have a very clear answer to help you understand assync and when you can run you code afterwards: Async/Await/then in Dart/Flutter

MarcoFerreira
- 195
- 1
- 14