Skip to content

support initialize Ref directly #5314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

yantaozhao
Copy link

@yantaozhao yantaozhao commented May 17, 2025

Description

Support initialize Ref with value directly.

Use case

In some scenarios, one control was created without known ref or the ref unavailable, but will be referenced in future.
It is more concise to direct initialize a ref with value rather than in a separate assignment statement.

Example:

# factory method to create and return a flet control
def control_factory(control_type: str) -> ft.Control:
    match control_type:
        case pattern_1:
            return ft.TextField(...)  # no ref here
        case pattern_2:
            return ft.Switch(...)
        case pattern_3:
            return ft.Slider(...)
        case ...:
            ......

controls: list[ft.Ref] = []  # list of ref to use later
control: ft.Control = control_factory(...)  # <-- create control in factory, with no ref passing into factory
# control_ref = ft.Ref()
# control_ref.current = control
control_ref = ft.Ref(control)  # <-- initialize directly instead of passing ref into factory or assigning it in another statement
controls.append(control_ref)

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Summary by Sourcery

New Features:

  • Support passing an initial value to Ref during construction instead of requiring a separate assignment.

@CLAassistant
Copy link

CLAassistant commented May 17, 2025

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants