Created
May 24, 2023 07:00
-
-
Save yuki2021/19cde00cc95204aaca16c9d57250b38b to your computer and use it in GitHub Desktop.
Completerで例外処理
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
void main() async { | |
Completer completer = Completer<String>(); | |
// 非同期処理を開始します(ここでは2秒待つだけ) | |
Future.delayed(Duration(seconds: 2)).then((_) { | |
// 非同期処理が完了したらエラーを投げます | |
completer.completeError(Exception("Something went wrong")); | |
}); | |
try { | |
// Completerの結果を待ちます | |
print(await completer.future); | |
} catch (e) { | |
print(e); // "Exception: Something went wrong" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment