flutter異步並行請求、串行請求

//多個異步並行處理 同步回掉
Future.wait([
  Future.delayed(new Duration(seconds: 2),(){
    return "hello";
  }),
  Future.delayed(new Duration(seconds: 4),(){
    return "world!";
  })
]).then((results){
  log(results[0]+results[1]);
}).catchError((e){
  print(e);
});

 


//串行處理數據
void task() async {
  try {
    String id = await login('158666666666', '1234556');
    print('登錄成功,用戶id爲${id}');
    String userInfo = await getUserInfo(id);
    print('獲取用戶信息成功,結果爲${userInfo}');
    saveUserInfo(userInfo);
    print('保存用戶信息成功');
  } catch (e) {
    print(e);
  }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章