flutter 中的Future方法讀取踩的小坑


flutter dart 語法中 Future其實和js中的Promse 原理是一樣的  Future 聲明的函數都是異步函數  

void testThen1() {
  Future f1 = new Future(() => null);
  Future f2 = new Future(() => null);
  Future f3 = new Future(() => null);

  f1.then((_) => print("f1 -> f1"));
  // f2 then 異步回調里面還有異步回調
  f2.then((_) {
    print("f2 -> f2");
    f1.then((_) => print("f2.then -> f1"));
  });
  f3.then((_) => print("f3 -> f3"));
}

//打印結果
f1 -> f1
f2 -> f2
f2.then -> f1
f3 -> f3

 

 

當遇到Future聲明的函數時候 想要獲取其return 返回值  必須也用異步的方法 否則只能讀取外層的Future

Future<bool> demo() async{
    await return true;
}

//用異步的方法讀取
demoRead() async{
 await demo();//打印true
}

//或者用then 的方法去接受 獲取
demo.then((e) {
    print("e");
    
  });

  

 

 dart 的語法還是多看看官方的解釋


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM