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

    Records

    Storage for structured records — logs, notes, any lists that grow over time. Unlike `Storage` , where one key holds one value, Records gives records stable id s, dates and soft deletion, so a single record can be edited or deleted without rewriting the whole list.

    Type: multi-child (a widget generator, supports lifecycle slots)

    Records collection:"workouts" data:@rows count:@n
      ListView.builder id:ready itemCount:$n
        @Macros id:itemBuilder index:0
          Text {rows[index]['title'] ?? ""}

    The rule of choice: if items are ADDED over time, it is Records . A scalar the user sets once (theme, daily goal, a flag) stays in Storage .

    Parameters

    Parameter Type Default Description
    collection string — Collection name. Alias — name
    includeDeleted bool false Also publish soft-deleted records (a trash screen)
    data @ variable — List of records
    count @ variable — How many records are published
    loaded @ variable — true when reading has finished
    error @ variable — Error text or null

    Every record always has the service fields id , createdAt , updatedAt , deletedAt — they are set automatically, you do not need to write them.

    Children

    Children are marked by loading state, and only the matching one is mounted — so id:ready is never built on missing data:

    Records collection:"notes" data:@notes count:@n
      CircularProgressIndicator id:loading
      Text "Could not read" id:error
      ListView.builder id:ready itemCount:$n
        @Macros id:itemBuilder index:0
          Text {notes[index]['title'] ?? ""}

    id:ready can be omitted — the first unmarked child becomes it.

    Writing: the ambient records object

    Reading is an element, writing is methods: an expression has no assignable left-hand side. Call them from a handler of the form (){…} ; a bare {records.add(…)} is a VALUE EXPRESSION, it runs once when the screen is mounted.

    Method What it does
    records.add(collection, fields) add a record
    records.update(collection, id, patch) change one record; an unknown id does nothing
    records.remove(collection, id) SOFT delete of one record (sets deletedAt )
    records.purge(collection, id) HARD delete of one record
    records.removeWhere(collection, (r){…}) soft delete ALL records matching the predicate
    records.purgeWhere(collection, (r){…}) hard delete all matching records
    records.clear(collection) clear one collection
    records.clearAll() clear ALL collections

    Full signatures and documentation are available through reeel_schema {name:"records"} .

    Examples

    A form that saves

    @title:""
    Records collection:"workouts" data:@rows
      Column
        TextField value:$title
        FilledButton onPressed:(){records.add("workouts", mapOf("title", title, "at", now())); title=""}
          Text "Add"

    Deleting by a condition

    The method chooses the scope of deletion: one record — remove / purge , an arbitrary filter — *Where , the whole collection — clear .

    // reset everything for today — one call, one pass
    OutlinedButton onPressed:(){records.removeWhere("intake", (r){dateKey(r['at'] ?? 0) == dateKey(today())})}
      Text "Reset today"

    Trash and restore

    remove is soft, so what was deleted is still available with includeDeleted:true . The purgeWhere predicate is the only one that SEES soft-deleted records:

    Records collection:"notes" includeDeleted:true data:@trash
      Column
        TextButton onPressed:(){records.purgeWhere("notes", (r){r['deletedAt'] != null})}
          Text "Empty the bin"

    Restoring is an ordinary update : records.update("notes", id, mapOf("deletedAt", null)) .

    A number from an input field

    TextField publishes a string even with keyboardType:number , and sum / sumBy skip strings: a volume written as text silently sums to zero. Convert on write — mapOf("volume", num(volume), …) — or declare the field in the schema: Field "volume" type:number converts the value on every write and read, and strings already saved are read as numbers.

    A chart from records

    Records are sparse and dated, while a chart needs a dense series of numbers. series(...) walks every day of the range, putting 0 where there were no records:

    Records collection:"intake" data:@rows
      State id:ready @week:{series(rows, addDays(today(), -6), today(),
                          (r){dateKey(r['at'])}, (rs){sumBy(rs, (x){x['ml'] ?? 0})})}
        SizedBox height:150
          $Bars values:$week color:$theme.primary

    Bad practices

    A pure function for a side effect

    // BAD — map/filter are PURE functions. Such a call produces a separate
    // write per row, they race each other, and the screen sees only the last one.
    onPressed:(){map(filter(rows, (r){dateKey(r['at']) == dateKey(today())}), (r){records.remove("intake", r['id'] ?? "")})}
    // GOOD — one call with a predicate: the whole selection, one publication
    onPressed:(){records.removeWhere("intake", (r){dateKey(r['at'] ?? 0) == dateKey(today())})}

    A growing list in a single Storage key

    // BAD — items have no id, editing one rewrites the whole list
    Storage @workouts:[]
    // GOOD
    Records collection:"workouts" data:@rows

    Subscribing without a default value

    // BAD — the generator's outputs are null until the data has loaded
    Text {rows[index]['title']}
    // GOOD
    Records collection:"workouts" data:@rows count:@n
      ListView.builder id:ready itemCount:$n
        @Macros id:itemBuilder index:0
          Text {rows[index]['title'] ?? ""}

    Best practices

    • Do not store derived values. Totals, series and streaks are computed from the records with an expression; a saved copy will sooner or later drift from the log.
    • Pass an object through the route , not through a global variable: the id as a path parameter, the object itself as the second argument of navigator.show(...) , read it through RouteData .
    • records.clear() without a collection name does nothing — the name is required.
    • clearAll is irreversible and fires on a single tap: put it behind a confirmation.

    See also