build method 와 render tree를 모르고 hot reload를 논하지마라

2024. 9. 25. 16:18·Flutter/Flutter

핫리로드가 될 때 화면이 아예 다시 그려지는건가? 라는 의견이 나왔다

 

아닌 것 같은데 왜 아니지 !!!!!!!!!!!!!

설명을 똑바로 못했다

 

왜 설명을 못했을까. 내 문제점은 다음과 같았다

 

1.빌드메서드가 정확히 뭘 하는지 몰랐다.
2.핫리로드랑 빌드메서드의 연관관계를 몰랐다.

 

그렇다면 알아보자 ㄱㄱ

 

The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.
 

canUpdate method - Widget class - widgets library - Dart API

bool canUpdate( Widget oldWidget, Widget newWidget ) Whether the newWidget can be used to update an Element that currently has the oldWidget as its configuration. An element that uses a given widget as its configuration can be updated to use another widget

api.flutter.dev

 

이는 build 메서드가 위젯 트리를 다시 그린다는 뜻으로, 기존의 위젯 트리(subtree)를 업데이트하거나 제거한 후 새로운 트리를 구성한다는 의미이다 

그니까. 빌드 메서드는 위젯트리를 그려주는 애인거다.

 

더보기

build() 를 처음 접할 때 흔히 프로그래밍에서 말하는 빌드, "앱 전체를 다시 컴파일하거나 생성한다"는 의미로 혼동했었는데, 헷갈리지말자.

빌드메서드다! 플러터에서 build() 메서드는 이 메서드 호출을 통해 위젯 트리를 구성하는 것으로 사용된다. 혼동금지 !!

 

build() 메서드는 Flutter의 UI를 구성하는 핵심 메서드로, 위젯이 어떻게 화면에 그려져야 하는지를 정의한다.

위젯이 화면이 어떻게 그려져야 하는지 알려주는 총 세가지의 요소가 있다. 

 

  • 위젯 트리는 UI의 설계도처럼 어떤 위젯들이 어떻게 배치될지를 설명함
  • 엘리먼트 트리는 이 위젯 트리를 기반으로 생성되어 위젯의 상태를 관리하고 업데이트하는 역할을 함
  • 렌더링 트리는 엘리먼트 트리의 정보를 기반으로 화면에 그릴 요소들을 실제로 렌더링하는 역할을 수행함.

 

 

Flutter의 모든 위젯은 화면에 그려지기 위해 렌더링 단계가 필요하고, 이때 위젯이 실제로 화면에 표시되는 과정은 Render Tree를 통해 이루어진다.

 

그렇다면 빌드메서드 호출은 화면 전체를 다시 그리는걸까?

NO❗️

 

아까도 언급했듯이 build() 메서드가 호출된다는 것은 위젯 트리(Widget Tree)를 다시 생성한다는 의미다.

하지만 이것이 실제로 화면 전체를 다시 그린다는 것은 아니다

렌더링 단계를 통해 위젯이 어떻게 화면에 표시될지 결정하는 과정을 거치고 마지막으로 렌더 트리(Render Tree)에서는 변경된 부분만 업데이트하고 다시 그려주는것이다. 

 

 

결론

 

핫리로드는 빌드메서드만 호출한다 -> 빌드메서드는 위젯트리를 다시 생성한다 -> 위젯트리는 렌더트리를 통해 화면에서 '변경된 부분'만 다시 그린다 -> 전체를 다시 그려주는 것이 아니다 

 


참고 문서 

 

https://blog.logrocket.com/complete-guide-flutter-architecture/
https://docs.flutter.dev/resources/architectural-overview

 

BuildContext class - widgets library - Dart API

A handle to the location of a widget in the widget tree. This class presents a set of methods that can be used from StatelessWidget.build methods and from methods on State objects. BuildContext objects are passed to WidgetBuilder functions (such as Statele

api.flutter.dev

 

 

Flutter architectural overview

A high-level overview of the architecture of Flutter, including the core principles and concepts that form its design.

docs.flutter.dev

 

 

A complete guide to Flutter architecture - LogRocket Blog

Learn about Flutter's architecture to help when structuring an app, updating state, building widgets or screens, and maintaining the app.

blog.logrocket.com

 

'Flutter > Flutter' 카테고리의 다른 글

webRTC 실험요약  (0) 2024.10.16
STFUL Lifecycle  (1) 2024.09.25
플러터 프로젝트에서 DI 주입은 어떻게 이루어지는가  (1) 2024.09.19
Provider 기초개념정리 + 예제  (2) 2024.09.18
1. 앱에 어울리는 애니메이션 구현하기 <Lottie편>  (1) 2024.09.16
'Flutter/Flutter' 카테고리의 다른 글
  • webRTC 실험요약
  • STFUL Lifecycle
  • 플러터 프로젝트에서 DI 주입은 어떻게 이루어지는가
  • Provider 기초개념정리 + 예제
복복씨
복복씨
개발자여, 사고하라 !
  • 복복씨
    정리노트
    복복씨
  • 전체
    오늘
    어제
    • 분류 전체보기 (118)
      • 개발새발자 (21)
        • 의 삶 (7)
        • 의 회고 (9)
        • 의 낙서장 (5)
        • 영어 (0)
      • FrontEnd (1)
        • React (1)
      • Flutter (38)
        • 새싹 (5)
        • Dart (8)
        • Flutter (14)
        • iOS 에서 Flutter 로 전환하며 (2)
        • 챗지피티랑놀.기 (3)
        • 하루 한 입 플러터 (2)
      • CS (7)
        • 짤막지식 (6)
      • IOS (6)
        • Swift (0)
        • UIKit (1)
        • SwitUI (4)
      • 머신러닝-딥러닝 (28)
        • 논문리뷰 (3)
        • study (16)
        • Kaggle (9)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    expando
    unawaited
    용산캠
    플러터 새싹
    dart
    테킷 앱스쿨
    runzonedguarded
    깊은참조
    schedulemicrotask
    IOS
    flutter 애니메이션
    플러터 di
    플러터
    한주 회고
    flutter lottie
    시그널링데이터
    멋쟁이 사자처럼
    새싹 용산
    swiftui 플러터
    새싹
    사그널링서버
    새싹 플러터
    부트캠프
    코드 결합도
    futurerecord2
    핫 리로드
    initState()
    Flutter Lifecycle
    FLUTTER
    getit
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
복복씨
build method 와 render tree를 모르고 hot reload를 논하지마라
상단으로

티스토리툴바