반응형
위젯들 간에 서로의 상태값을 가져오는 방법은 다음과 같습니다.
- 상위 위젯에서 currentState를 사용해서 하위 위젯의 상태값 가져오기 (하위위젯 생성시 키를 사용)
class ParentWidgetState extends State<ParentWidget> {
GlobalKey<ChildWidgetState> childKey = GlobalKey();
// Child 생성
@overrider
Widget build(BuildContext context) {
return ChildWidget(key: childKey);
}
// Child 상태값 사용
...
ChildeWidgetState? childState = childKey.currentState;
childState?.childValue
...
}
- 하위 위젯에서 findAncestorStateOfType을 사용해서 상위 위젯의 상태값 가져오기
ParentWidgetState? state = context.findAncestorStateOfType<ParentWidgetState>();반응형
'Dev Study > Flutter & Dart' 카테고리의 다른 글
| Flutter 상태관리 완전 가이드 (Part 3/3) (0) | 2025.11.05 |
|---|---|
| Flutter 상태관리 완전 가이드 (Part 2/3) (0) | 2025.11.05 |
| Flutter 상태관리 완전 가이드 (Part 1/3) (0) | 2025.11.05 |
| Flutter 빌드는 되는데 실행할때 오류나는 경우 (0) | 2025.11.04 |
| Dart 상속 (0) | 2023.11.17 |
| Dart Class 생성자 (0) | 2023.11.17 |
| Dart 예외처리 (try on finally) (0) | 2023.11.16 |
| Dart Function (0) | 2023.11.16 |

