Future<String>//곧 String 받음
Future<int>//곧 int 받음
Future<double>//곧 double 받음
Future<String> fetchData() async{
return Future.delayed(const Duration(seconds: 2), () => 'Data');
}
Future<String> fetchUser() async{
String userName = await Future.delayed(const Duration(seconds: 2), () => 'I am Groot');
return userName;
}
void main() async{ String name = await fetchUser(); print(name);}
String createOrderMessage(){
var order = fetchUserOrder();
return 'your order is: $order';
}
Future<String> fetchUserOrder(){
return Future.delayed(const Duration(seconds: 2), () =>'Large Latte');
}
void main(){
print('Fetching user order...');//(1)
print(createOrderMessage());//(2)
}
실행결과
1. 'fetching user order...'
2. 'Your order is: Instance of '_Future''
Future<String> createOrderMessage() async{
print('Start');//(2)
var order = await fetchUserOrder();//2초 기다림
return 'your order is: $order';
}
Future<String> fetchUserOrder(){
return Future.delayed(const Duration(seconds: 2), () =>'Large Latte');}
void main() async{
print('Fetching user order...');//(1)
print( await createOrderMessage());//(3)
}
실행결과
1. 'fetching user order...'
2. 'Start'
3. 'Your order is: Large Latte'
void main() async{
print('Before the Future');//(1)
await Future((){
print('Running the Future');//(2)
}).then((_){
print('Future is complete');//(3)
});
print('After the Future');//(4)
}
try{
List<int> a = [1,2,3];
print(a[3]); //예외발생시 출력x
} catch(e){
print("숫자가 존재하지 않습니다");
}
void main() async{
try{
String data = await fetchData();
print(data); //예외발생시 출력x
} catch (error){
print('데이터 처리 중에 에러가 발생했습니다');
}
}
Future<String> fetchData(){
return Future.delayed(const Duration(seconds: 2), (){
return 'Data';
});
}
Future<int>//기다렸다가 사용 가능한 단일 데이터
Stream<int>//기다렸다가 사용 가능한 복수 데이터