Flutter/Dart not waiting for response

At the point when you’re new to Dart/Flutter this can get truly confounding, however when you run not many models it doesn’t look dubious any longer.

Async implies that this capacity is offbeat and you may need to stand by a piece to get its outcome.

Anticipate in a real sense implies – stand by here until this capacity is done and you will get its bring an incentive back.

Future is a sort that ‘comes from the future’ and returns an incentive from your nonconcurrent work. It can finish with success(.then) or with

an error(.catchError).

.Then((value){… }) is a callback that is called when future finishes successfully(with a worth).

void main() async {
  print(getMeSomeFood());         
  print(await getMeSomethingBetter());       
  maybeSomethingSweet().then((String value) {
    print(value);                    
  });
}
Future<String> getMeSomeFood() async {
  return "an apple";
}
Future<String> getMeSomethingBetter() async {
  return "a burger?";
}
Future<String> maybeSomethingSweet() async {
  return "a chocolate cake!!";
}
Future<String> bye() {          //will not compile, add async
  return "see you soon! :)";
}

This is the output:


Instance of ‘_Future’
a burger?
a chocolate cake!!


As you could see, there’s no difference between .then and await when it comes to fetching results. But be careful, when you use await, your program will wait there until async function finishes:

void main() async {
  await waitForMe();
  print('I was waiting here :)');
}
Future waitForMe() async {
  print('Started.');
  return Future.delayed(Duration(seconds: 5), () {
    print("Now I'm done!");
  });
}

This is the yield:

Begun.

Presently I’m finished!

I was holding up here 🙂

Also, this is a model where we would prefer not to stand by:

void main() async {
  waitForMe().then((_) {
    print("I'm more done THEN you :)");
  });
  print('I was waiting here :)');
}
Future waitForMe() async {
  print('Started.');
  return Future.delayed(Duration(seconds: 3), () {
    print("Now I'm done!");
  });
}

This is the yield:

Begun.

I was holding up here 🙂

Presently I’m finished!

I’m more done THEN you 🙂

As some of you were pondering WHEN precisely to utilize every one of these words, this is the overall clarification:

When to utilize async? At the point when you need to characterize an async strategy.

When to utilize anticipate? At the point when you have to trust that the strategy will complete and afterward continue with your code execution.

When to utilize at that point? At the point when you need to deal with Future after it was effectively completed in an async way – program will proceed with execution after this async technique was called, yet .at that point() callback will be executed later.

When to utilize Future? At the point when you need to get an outcome from an async work.

Simply apply those examples to your particular issues in the code.

Preparing for the Flutter interview? 🧐

What is a (fat)arrow punctuation in Dart?

=> articulation

For capacities that contain only one articulation, you can utilize short form with => that essentially does {return expression;}

void main() {
  function1(1);   //arg was not 3
  function2(1);   //arg was not 3
  function1(3);   //arg was 3
  function2(3);   //arg was 3
}
void function1(int a) {
  if (a == 3) {
    print('arg was 3');
  } else {
    print('arg was not 3');
  }
}
void function2(int a) => print('arg was ${a == 3 ? '' : 'not '}3');

Ripple INTERVIEW QUESTIONS

What’s the distinction among const and last in Dart?

Both last and const keep a variable from being reassigned and const factors are verifiably last.

Along these lines, when you allot an incentive to a const or last factor, you can not change it.

A const variable is an aggregate time steady, which implies that it must be made from constants accessible in order time.

void main() {
  const int a = 3;
  final int b = 4;
  const int c = a * 9;
  const int d = a * b; //this will not compile
  final DateTime now = DateTime.now();
  const DateTime anotherNow = DateTime.now(); //this will not compile
  a = 5; //this will not compile
  b = 6; //this will not compile
}

Vacillate INTERVIEW QUESTIONS

What’s the contrast among var and dynamic sort in Dart?

On the off chance that a variable is proclaimed as a dynamic, its sort can change over the long run.

dynamic a = 'abc'; //initially it's a string
a = 123; //then we assign an int value to it
a = true; //and then a bool

In the event that you announce variable as a var, when alloted type can not change.

var b = 'cde'; //b is a string, and its type can not change
b = 123; //this will not compile 
         //can not assign an int to the string variable

Yet, in the event that you express a var without instating, it turns into a dynamic:

var a; //this is actually a dynamic type
a = 2; //assigned an int to it
a = 'hello!'; //assigned a string to it
print(a); //prints out 'hello'

… and if you liked this article, please clap few times 🙂

Leave a Comment