State and lifecycle
Elements with no picture of their own: they declare variables for their subtree (
State
,
Storage
), run an action when the screen appears or when data changes (
Initializes
,
Subscribes
), and repeat a template among the parent's children (
Repeat
). Everything they wrap is drawn as usual.
Do not confuse them with
*State
and
*Repeat
— those are words of the drawing layer and work only inside a
*
chain (see
Render primitives
). The variable syntax (
@
,
$
,
{…}
) is in the
README
.
State
Declares reactive
@
variables for its subtree and draws its single child unchanged. Every descendant reads a variable as
{name}
,
$name
or
${name}
in a template string; a write (
{name = …}
) redraws everyone who reads it.
Type:
wrapper
State @count:0
Column spacing:8
Text $"Pressed: ${count}"
FilledButton onPressed:{count = count + 1}
Text "+1"
| Parameter | Type | Default | Description |
|---|---|---|---|
@name:value
|
any | — |
A subtree variable. The value is a literal (
@n:0
,
@tags:[]
) or an expression
@name:{…}
, evaluated once when the screen is prepared
|
State
has no other parameters.
State
is for computing a value
once
and reading it in several places, instead of hanging an
@
variable on an unrelated element:
State @prices:[120, 80, 150]
State @top:{max(prices)}
Column spacing:4
Text $"Maximum: ${top}"
Text $"Items: ${length(prices)}"
A variable
shadows
the expression function of the same name: name a variable
max
and the subtree loses the
max(…)
function. Hence
@top
, not
@max
.
State @count:0
Row spacing:12
FilledButton onPressed:{count = count + 1}
Text "+1"
OutlinedButton onPressed:{count = 0}
Text "Reset"
Text $"Pressed: ${count}"
Storage
Like
State
, but the values
survive a restart
: when it appears, the element substitutes the saved value for the one written in the markup, and saves every write. The storage key is the variable name; the same key declared on different screens is one value.
Type:
wrapper
Storage @visits:0
TextButton onPressed:{visits = visits + 1}
Text $"Opened times: ${visits}"
| Parameter | Type | Default | Description |
|---|---|---|---|
@name:value
|
any | — | A saved variable; the value in the markup is what applies while nothing has been saved |
scope
|
device
|
session
|
device
|
How long the value lives.
device
— across launches.
session
— one visit: a browser tab on the site, one launch in the app
|
Lists and maps are saved transparently: the variable holds a live list (indexes work right away), the storage holds JSON. Change a collection with functions that return a new one (
toggle
,
add
,
remove
), not in place:
@productId:"p1"
Storage @favorites:[]
IconButton onPressed:{favorites = toggle(favorites, productId)} tooltip:"Add to favorites"
Icon {contains(favorites, productId) ? "favorite" : "favorite_border"}
scope:"session"
is for "show once per visit". Saved forever, this would turn into "once ever", and a person coming back a month later would not see what they saw the first time:
Storage @bannerClosed:false scope:"session"
Column
TextButton onPressed:{bannerClosed = true} if:{!bannerClosed}
Text "New in the catalog — close"
Text "Catalog"
A growing collection (a log, notes, entries by date) is
Records
, not a list under one
Storage
key.
Initializes
Runs
run
once
, when the element appears on screen, and draws its child. The body's dependencies are not tracked: a body that reads
token
does not run again when
token
changes.
Type:
wrapper
State @openedTimes:0
Initializes run:{openedTimes = openedTimes + 1}
Text $"Screen opened: ${openedTimes}"
| Parameter | Type | Default | Description |
|---|---|---|---|
run
|
callback | — |
What to run.
(){…}
if the body
calls
something (
records.add
,
navigator.show
);
{…}
if it assigns
|
A value that should be
computed
once rather than run, declare as a variable:
State @openedAt:{now()}
.
Subscribes
Runs
run
every time something the body
reads
changes, and draws its child. The dependencies are derived from the body, not listed by hand: a list written by hand silently goes stale when the body is edited.
Type:
wrapper
State @query:"" @log:""
Subscribes run:{log = "Searching: " + query}
Column spacing:8
TextField onChanged:(value){query = value}
Text $log
| Parameter | Type | Default | Description |
|---|---|---|---|
run
|
callback | — |
The reaction. Same rule as for
Initializes
: a call —
(){…}
, an assignment —
{…}
|
There is exactly one difference from
Initializes
: that one fires once and does not see dependencies, this one follows them.
Bad practices
// BAD — `{…}` around a CALL: a value computed at parse time, no reaction
@status:""
Subscribes run:{status == "signedOut" ? navigator.show("/signin") : null}
Text "Account"
// GOOD — the call is in `(){…}`
@status:""
Subscribes run:(){status == "signedOut" ? navigator.show("/signin") : null}
Text "Account"
Repeat
Repeats a
@Macros
template
in its own place
among the parent's children. The layout is chosen by the parent (
Column
,
Row
,
Wrap
,
Stack
,
GridView
…);
Repeat
only repeats. The template gets
index
(from 0) and
count
; the template reads the list item itself from scope (
chips[index]
) — there is no implicit
item
, just as with
`*Repeat`
.
Type:
single-child
(a
@Macros
template)
State @chips:["New", "Sale", "Bestsellers"]
Wrap spacing:8
Repeat count:{length(chips)}
@Macros id:itemBuilder index:0
Chip
Text id:label {chips[index]}
| Parameter | Type | Default | Description |
|---|---|---|---|
count
|
int |
length of
values
|
How many iterations. Negative or non-numeric means zero iterations |
values
|
list | — |
A list whose length is the number of iterations when there is no
count
|
The template is one
@Macros
(with
id:itemBuilder
or without). Without a template, or under a parent that does not expand the loop (for example,
Padding
),
Repeat
draws nothing; in a debug build a visible error appears in its place.
The number of copies is limited: iterations × template elements (together with enclosing
Repeat
s) must not exceed the tree's copy limit. Beyond the limit the loop is not expanded
at all
— half a list would look like data that ran out.
The repetitions sit among ordinary children, so they can be mixed with static ones:
State @steps:["Cart", "Delivery", "Payment"]
Column crossAxisAlignment:start spacing:6
Text "Checkout"
#style fontWeight:w700
Repeat values:$steps
@Macros id:itemBuilder index:0
Text $"${index + 1}. ${steps[index]}"
State @tags:["New", "Sale", "Bestsellers", "Gifts"]
Wrap spacing:8 runSpacing:8
Repeat count:{length(tags)}
@Macros id:itemBuilder index:0
Chip
Text id:label {tags[index]}
Differences between the app and the site
-
State— the variables become page state; a write from a handler redraws the readers in the browser. -
Storageon the site keeps values inlocalStorage, andscope:"session"in the tab'ssessionStorage. The page is built with the value from the markup; the saved one is substituted in the browser. -
InitializesandSubscribeswork: the body is translated into the page script and runs when the page loads (Initializes) or when what it reads changes (Subscribes). A body that could not be translated does not run on the site, and the editor warns about it. A value the body writes does not get into the page's HTML — the script sets it. -
Repeaton the site is expanded when the page is built : as many iterations as there were then, so many there will stay. Ifcount/valuesread data that changes in the browser (aStatea button writes,Storage, anHttpDataresponse), the editor warns, and the list does not grow. For such a list useListView.builder itemCount:… shrinkWrap:true(see ListView ).
See also
-
Render primitives
—
*State,*Repeatof the drawing layer - Records — growing collections
-
Chain and Macros
— flat wrapper notation, the
@Macrostemplate -
Data generators
—
JsonData,FilterData,MapValue