Skip to content

Instantly share code, notes, and snippets.

@yuki2021
Created May 24, 2023 07:00
Show Gist options
  • Save yuki2021/19cde00cc95204aaca16c9d57250b38b to your computer and use it in GitHub Desktop.
Save yuki2021/19cde00cc95204aaca16c9d57250b38b to your computer and use it in GitHub Desktop.
Completerで例外処理
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