skip to Main Content

I’m currently new to Flutter and Dart. I have functions that need to be finished before another function is executed. Is it reasonable to use Future Delayed to delay that specific function? Is there another method of doing it?

The function that I want to delay needs specific data from the previous function, and when it executes, it returns null values. Now, I want it to finish the first function before the following function executes.

3

Answers


  1. No as you said it isn’t reasonnable to use the Future.delayed!

    Why? Because you will need to set a Duration and then there’s 2 options:

    1- the duration is too short, and the first function will not be finished

    2- the duration is too long, and the user will wait (for nothing)

    As the first function takes time (I suppose it’s a network call or a storage call), it probably returns a Future<Something> and is async. You have then 2 options:

    1- Use a callback when the Future is completed

      yourFirstFunction().then((value) {
        // Do something with the returned value
      });
    

    2- Use await keyword

      final value  = await yourFirstFunction();
      // Do something with the returned value
    

    It’s a matter of preference. My own preference is to use linear code style and use await keyword.

    You can lean more on Future and await here: https://dart.dev/codelabs/async-await

    Login or Signup to reply.
  2. That is not how Future is supposed to work. Delays, will not ensure that the previous processes have finished, for that, you need to create either a listener or another type of controller to track if the first processes ended, or if they returned a valid response.

    What you can do is this.
    Here is one possible logic structure, to define your own process:

    • Call First process. When the process ends, set boolean _first_process_ended as true;
    • Call the Second process. When the process ends, set boolean _second_process_ended as true;
    • If _first_process_ended == true and _second_process_ended == true launch Third process.

    Another possibility is to use Future methods:

    _myfutureCall
    .then((result) { /* return of the Future call */ })
    .catchError((err) { /* error call */ })
    .whenComplete(() { /* do something after future ended */ });
    
    Login or Signup to reply.
  3. No. Using delay isn’t a guaranteed or efficient way to do it.
    Why? Because the first async function could complete before or after the delay duration depending on system, network and other factors.

    Solution?
    Use then() or await on both functions.

    1.

    main(){
    
    // you can await this as well, depends on where you invoke.
     func1().then(
    //you can return values required in func2 in func1 which can be accessed by `value` .
       (value)=>func2(),
    );
    
    }
    
    main() async{
    
    await func1();
    await func2();
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search