Development/Flutter
GetView / Obx를 통한 ListView 사용법
독행소년
2021. 7. 19. 11:33
Obx를 통해 위젯의 상태값을 추적할 때, 기본적으로 단일 위젯의 상태값을 바라보게 된다.
무슨 말이냐면, ListView 위젯의 경우 children을 Obx를 래핑할 수 없다는 뜻이다.
우선 ListView의 children에 들어갈 데이터 리스트는 다음과 같이 추가하고 UI에 반영할 수 있다.
historyList.value.addAll(
_payload.dataList.map((data) => History.fromJson(data)).toList());
ac.historyList.refresh();
ListView의 경우 일반적인 방식으로 children을 Obx로 래핑할 수 없으며, 아래와 같이 ListView.builder를 이용하면서 이를 Obx로 래핑해야 한다.
class HistoryListView extends GetView<AppController> {
@override
Widget build(BuildContext context) {
return Obx(
() => ListView.builder(
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount: controller.historyList.value.length,
itemBuilder: (BuildContext context, int index) {
return Text(controller.historyList.value[index].carNumber);
},
),
);
}
}