How do I get the value of this variable outside the .then((){}); statement in flutter?
1,356
If you use the .then()
clause you need to do all your work inside it and you can't use its values outside because you don't know when you are receiving the answer.
You could await
the answer there and mark the method that encloses your code with async
.
bool check;
final docs = Firestore.instance.collection('tests').where('name',isEqualTo: snapshot['name'].replaceAll('.mp4','.txt')).getDocuments();
if(docs.documents[0].exists)
check = true;
else
check = false;
debugPrint(check.toString());
or
Future<void> doSomething() async {
bool check;
final docs = Firestore.instance.collection('tests').where('name',isEqualTo:
snapshot['name'].replaceAll('.mp4','.txt')).getDocuments();
if(docs.documents[0].exists)
check = true;
else
check = false;
debugPrint(check.toString());
}
Author by
Coder
Updated on December 08, 2022Comments
-
Coder over 1 year
I have the following lines of code
bool check; Firestore.instance.collection('tests').where('name',isEqualTo: snapshot['name'].replaceAll('.mp4','.txt')).getDocuments().then((docs){ if(docs.documents[0].exists) check = true; else check = false; }); debugPrint(check.toString());
This produces an error saying that a bool can't have a value of null, so how do I retrieve the value of
check
from inside the.then
statement so that I can use it outside of the.then
statement? -
pskink over 5 yearswhy would
await
block UI thread? in dart there is only one, UI thread so any use ofawait
would block it? it simply suspends the execution, and not blocks the whole thread -
chemamolins over 5 yearsI have rephrased my answer. It is not possible to infer from your question where your code is located. If you can't use an
await
then you must use thethen()
but then you can't reliable access the variable inside from outside.await
can be used without blocking the ui if the method call is marked withasync
and is used accordingly. -
chemamolins over 5 yearsCould you provide more context to the question?
-
pskink over 5 years
await
andthen
are really the same things - and no, you cannot useawait
in a method that is notasync
-
chemamolins over 5 yearsTelling that
await
andthen
are the same thing is a bit misleading. They are two different ways of handling the same asynchronous response. The end result is the same but through different paths. -
pskink over 5 yearsi agree - different paths - but in any way, neither
await
northen
blocks UI thread as you wrote in your answer -
chemamolins over 5 yearsRight. Async call don't block the ui per se. What I meant is that, depending on how and where you call your async methods with await, you could block the ui.
-
pskink over 5 yearsfortunately
await
will never block ui thread, see dartlang.org/articles/language/await-async or similar articles -
chemamolins over 5 yearsI understand what you mean really mean but that is only true if you use await/async correctly. In the gist gist.github.com/jmolins/cd426981fa3f675aed55b035cb63d4b4 you can see that the ui is easily blocked with an incorrect call.
-
Richard Heap over 5 yearsNo, it does not block the UI. Try tapping the FAB multiple times. Each time you see the ink ripple, and after 4 seconds the counter increments N times. The UI is perfectly functional and responsive; you've just asked it to change the counter after 4 seconds - which it does.
-
chemamolins over 5 yearsYes. You';re right. I see what you mean. The UI is not blocked. But depending on how you use the await, the UI may not be updated as you expect. At least in this case. Thanks for the patience.
-
pskink over 5 yearsas Richard said the UI is not blocked: think of
var x = await future; print(x);
as:future.then((x) => print(x));
- this is really the same code