Home > Mobile >  How to send data from one future task to another future task in flutter?
How to send data from one future task to another future task in flutter?

Time:06-23

For example there are 4 Future tasks in flutter that are dependent on each other and after these tasks are completed Navigator.pushReplacement is called to change function. By dependent it means that return value of one Future is the argument of other Future task.

Future<int> f1() async{}
Future<String> f2(int i) async{}
Future<bool> f3(String s) async{}
Future<int> f4(bool b) async{}

The argument of f2 is return value of f1, argument of f3 is return value of f2 and argument of f4 is return value of f3. Which means f2 should not be executed until f1 is completed, f3 should not be executed until f2 is completed and f4 should not be executed until f3 is completed. After these all are completed Navigator.pushReplacement should be called to change page. How do I achieve this?

CodePudding user response:

  Future<int> f1() async{
    int i=1;
    return i;
  }
  Future<String> f2(int i) async{
    String string='string';
    return string;
  }
  Future<bool> f3(String s) async{
    bool val=true;
    return val;
  }
  Future<int> f4(bool b) async{
    int i=1;
    return i;
  }
  ///create a function to execute the above 4 futures in order
  function()async{
    f1().then((f1_return) {
      f2(f1_return).then((f2_return){
        f3(f2_return).then((f3_return) {
          f4(f3_return).then((f4_value) {
            ///add your Navigator.pushReplacement method here
          });
        });
      });
    });
  }
  • Related