Dart의 MyComponent를 초기화하려면 서버에 HttpRequest를 전송해야한다고 가정 해 봅시다. 동 기적으로 객체를 구성하고 응답이 돌아올 때까지 '실제'초기화를 연기 할 수 있습니까?Dart의 구성 요소 생성자에서 비동기 메서드 호출
아래 예제에서는 "완료"가 인쇄 될 때까지 _init() 함수가 호출되지 않습니다. 이 문제를 해결할 수 있습니까?
import 'dart:async';
import 'dart:io';
class MyComponent{
MyComponent() {
_init();
}
Future _init() async {
print("init");
}
}
void main() {
var c = new MyComponent();
sleep(const Duration(seconds: 1));
print("done");
}
출력 :
done
init
정적 비동기 방식을 사용할 수 있습니까? – Ganymede