Skip to main content

@observable

Makes a class observable by mixing in Observable. Can be applied to any class including dataclasses.dataclass. For dataclasses, decorator can be placed either above or below the @dataclass decorator.

Example
from dataclasses import dataclass
import flet as ft


@ft.observable
@dataclass
class MyDataClass:
x: int
y: int
Note

List and dict values are copied into observable collections when they are assigned to a field or added to such a collection, so later changes to them notify the field. Assigning other.items to obj.items copies it too: the two fields don't share one list.

Assigning a field the mutable object it already holds, as Python does after an in-place operator such as obj.tags |= {"a"} on a set, notifies, since the object may have changed in place.

Listeners are called synchronously. With augmented assignments such as obj.items += [x], *= on lists and |= on dicts, they run before Python assigns the result back to the field, so a listener that replaces obj.items at that moment is overwritten. Direct calls such as obj.items.extend([x]) keep the listener's replacement.