Future<> 객체
- dart에서 비동기 함수일 경우, Future를 래핑해서 Response 반환하는데
- 이 Future를 이용해서 쉽게 UI를 구현할 수 있다.
UI 구현
- future로 래핑된 데이터를 사용해서 화면을 그릴 수 있다.
- snapshot에 데이터가 담겨오고 있음.
- snapshot.hasData를 통해서 데이터 존재 여부를 판단할 수 있다.
- 데이터를 아직 못받아왔을 경우, CircularProgressIndicator 위젯을 사용해서 로딩 중을 쉽게 구현 가능함.
- snapshot.data로 실제 데이터를 접근할 수 있음.
//.dart
FutureBuilder(
future: futureData,//future로 래핑된 데이터 변수.
builder: (context, snapshot) {//snapshot에 데이터가 담겨있음.
if(snapshot.hasData){
return Text("there is data");
}
return Center(
child: CircularProgressIndicator(),
);
}
);
flutter 졸라 좋음.
'lecture.js > flutter.log' 카테고리의 다른 글
#widget 001: PageView (0) | 2023.06.14 |
---|