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());
}
Share:
1,356
Coder
Author by

Coder

Updated on December 08, 2022

Comments

  • Coder
    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
    pskink over 5 years
    why would await block UI thread? in dart there is only one, UI thread so any use of await would block it? it simply suspends the execution, and not blocks the whole thread
  • chemamolins
    chemamolins over 5 years
    I 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 the then() 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 with async and is used accordingly.
  • chemamolins
    chemamolins over 5 years
    Could you provide more context to the question?
  • pskink
    pskink over 5 years
    await and then are really the same things - and no, you cannot use await in a method that is not async
  • chemamolins
    chemamolins over 5 years
    Telling that await and then 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
    pskink over 5 years
    i agree - different paths - but in any way, neither await nor then blocks UI thread as you wrote in your answer
  • chemamolins
    chemamolins over 5 years
    Right. 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
    pskink over 5 years
    fortunately await will never block ui thread, see dartlang.org/articles/language/await-async or similar articles
  • chemamolins
    chemamolins over 5 years
    I 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
    Richard Heap over 5 years
    No, 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
    chemamolins over 5 years
    Yes. 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
    pskink over 5 years
    as 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