Ask the assistant
0/400
Your question is processed by an AI service and kept to improve answers. Do not enter personal data.

    Data generators: JSON, filter, value table

    Generators publish data into variables you named with @ on the value side: data:@products creates a reactive products , visible to the whole subtree. They draw nothing of their own — what they wrap is drawn. JsonData reads a project file, FilterData filters a list into a new list, MapValue translates a value through a table. The time generator is TimeData , the network request one is HttpData .

    JsonData

    Reads a JSON file from the project's data/ directory and publishes its contents.

    Type: data (one child or the slots id:loading / id:error / id:ready )

    JsonData src:"data/products.json" data:@products count:@n
      CircularProgressIndicator id:loading
      Text "Could not read the file" id:error
      ListView.builder id:ready itemCount:$n
        @Macros id:itemBuilder index:0
          Text {products[index]['title'] ?? ""}
    Parameter Type Default Description
    src string — Path to the file from the project root: data/products.json
    file string — Same as src (read when there is no src ). Not read on the site — write src
    data output @name — The parsed JSON: a map or a list
    count output @name — Length of the list or number of keys in the map
    loaded output @name — true once the file is read (or not found)
    error output @name — Error text or null

    Fields are taken apart with indexes on the reader's side: {products[index]['price'] ?? 0} .

    Slots. With children id:loading / id:error / id:ready only the one that matches the state is mounted; id:ready is not built until there is data. The first child without an id is ready . Without slots the outputs are empty on the first frame, and every read needs ?? fallback .

    JSON Lines. A .jsonl file is read line by line into a list — one value per line. This is convenient for data that grows and is edited one record at a time (subtitles, transcript segments). A broken line is named in error by its number.

    Several JsonData with different output names nest inside each other without conflicts:

    JsonData src:"data/products.json" data:@products
      JsonData src:"data/config.json" data:@config
        Text id:ready {config['title'] ?? ""}

    FilterData

    Filters a list from another generator into a new list: the list widget receives the data already filtered, with no hiding of rows through Visibility . The condition is recomputed by itself when the source list changes or any variable that where reads changes (for example, a search string).

    Type: wrapper

    State @fruits:["apple", "pear", "plum"]
      FilterData from:$fruits where:(item){contains(item, "p")} data:@found count:@n
        Text $"Found: ${n}"
    Parameter Type Default Description
    from list — The source: a variable ( $products ) or an expression that yields a list ( {set['rows']} )
    where callback (item){…} — The condition: true — the item stays. The item arrives in the single declared argument, any name
    data output @name — The filtered list
    count output @name — Its length

    Without a list in from or without an (item){…} callback in where the filter publishes an empty list and 0 and reports this with a diagnostic.

    State @query:""
      JsonData src:"data/products.json" data:@products
        Column id:ready spacing:8
          TextField onChanged:(value){query = value}
          FilterData from:$products where:(item){contains(lowerCase(item['title'] ?? ""), lowerCase(query))} data:@filtered count:@total
            ListView.builder itemCount:$total shrinkWrap:true
              @Macros id:itemBuilder index:0
                Text {filtered[index]['title'] ?? ""}

    MapValue

    Translates an input value into an output variable through a table — instead of a chain of ternaries. It is recomputed on every change of the input.

    Type: wrapper

    State @status:"done"
      MapValue $status @label "new":"New" "done":"Done" "fail":"Error"
        Text $label

    The first positional argument is the input, the second is the output, then come "key":value pairs. The input is compared by its string form, so paths, statuses and numbers serve as keys ( 61 matches "61" ). The value is any scalar. If nothing is found, the first entry of the table is taken.

    Parameter Type Default Description
    (1st positional) / value any — Input: a variable or an expression
    (2nd positional) / to output @name — Where to publish the result
    "key":value — — Table rows
    #map structure — The table on a separate line, for long tables
    default any first entry The result when the key is not found

    The named form with #map reads better when the table is long:

    State @code:61
      MapValue value:$code to:@sky default:"Clear"
        #map "61":"Rain" "71":"Snow" "45":"Fog"
        Text $sky

    The opposite direction — an index into a value — is the function pick(index, a, b, …) .

    Differences between the app and the site

    • On the site JsonData reads the file when the page is built : the data is in the HTML right away, with no request from the browser. The slots work the same — a file that is not found gives the id:error slot. The site does not read the file: parameter: the outputs stay empty, with no error. Write src: .
    • FilterData with a condition that does not depend on the visitor's actions filters at build time — the HTML holds only the matching rows. If the condition reads a variable the page changes (a search field), the whole list goes into the HTML, and the browser hides and shows rows. count then stays the length of the full list and does not change in the browser.
    • MapValue is computed at build time for the first picture and recomputed in the browser when the input changes there.

    See also