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

    HTTP data

    HttpData requests JSON from an address and publishes the response into variables you name; HttpHeaders sets headers for those requests (a token, for example). For a file from the project use JsonData .

    HttpData

    A generator: it makes the request when it appears (and when the address changes), parses the JSON and writes it into its output @ variables. Children read the response by index: {weather['current_weather']['temperature'] ?? 0} .

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

    HttpData url:"https://api.example.com/items" data:@items
      CircularProgressIndicator id:loading
      Text "Failed to load" id:error
      Text id:ready $"Items: ${length(items)}"

    Request parameters

    Parameter Type Default Description
    url string — The address. Can be a template: $"…?q=${query}"
    method string GET The method, sent as written (in upper case)
    body string — Request body for everything except GET ; read at the moment of sending
    immediate bool true Request right away when it appears
    watch bool true Repeat the request when a reactive url has changed
    debounceMs int 300 Pause before a repeat on watch , ms: typing does not send a request on every letter
    cache bool false Show the previous response first, then replace it with a fresh one (see below)

    Outputs

    All are optional; declare only the ones you need ( data:@items ).

    Parameter Type Description
    data output @name The parsed JSON of the response (a map or a list). Only on success
    loaded output @name true when a response or an error has arrived; stays true afterwards
    pending output @name Whether a request is in progress now . Goes off on any outcome; two presses go off after the last response. For a spinner in a button
    error output @name Error text for a person, or null on success
    status output @name The HTTP status code; null if the request did not reach the server. For branching: if:{code == 401}
    errorData output @name The parsed body of a failure , if the server sent JSON; otherwise null. Separate from data : data means “it worked”
    refresh output @name A ready “request again” callback: onPressed:$reload

    State slots

    Instead of one child you can give children with id: — only the one matching the state is mounted. id:ready is not built until there is data, so a list in it never sees an empty first frame. The first child without an id counts as ready .

    HttpData url:"https://api.example.com/items" data:@items
      CircularProgressIndicator id:loading
      Text "Failed to load" id:error
      ListView.builder id:ready itemCount:{length(items)}
        @Macros id:itemBuilder index:0
          Text {items[index]['title'] ?? ""}

    cache:true

    The previous response appears at once, and the fresh one replaces it. The promise is the same in the app and on the site:

    • the cache is shown only while nothing else has been published — an update does not “flicker” back to the old one;
    • an entry remembers the address, method and body: a different request is not served from it;
    • an entry older than a day is a miss;
    • the whole cache is cleared when the identity changes: Session clears it on sign-in, sign-out and a rejected token refresh.

    Examples

    Search that repeats on address change

    State @query:""
      Column spacing:8
        TextField onChanged:(value){query = value}
        HttpData url:$"https://api.example.com/search?q=${query}" data:@found error:@err debounceMs:500
          LinearProgressIndicator id:loading
          Text id:error $err
          Text id:ready $"Found: ${length(found)}"

    Sending a form with a spinner in the button

    @name:"Anna"
    HttpData url:"https://api.example.com/feedback" method:"POST" body:$"{\"name\":\"${name}\"}" immediate:false refresh:@send pending:@busy status:@code
      Column spacing:8
        FilledButton onPressed:$send
          Text {busy ? "Sending…" : "Send"}
        Text "The server did not accept the request" if:{code != null && code >= 400}

    onPressed:$send passes the callback. onPressed:{busy ? null : send} is not a call but a value: the button is drawn and does nothing.

    HttpHeaders

    Sets headers for HTTP requests: each named parameter is a header with that name. The value is a string, a $"…" template or an expression; when a variable it reads changes, the header is updated. Draws its child.

    Type: wrapper

    State @token:"abc"
      HttpHeaders Authorization:$"Bearer ${token}"
        HttpData url:"https://api.example.com/me" data:@me
          CircularProgressIndicator id:loading
          Text "No access" id:error
          Text id:ready {me['name'] ?? ""}
    Parameter Type Default Description
    Header-Name:value string — A request header: Authorization:$"Bearer ${t}" , Accept-Language:"ru"

    The headers apply to all HTTP requests of the project, not only to the subtree: even after the element has left the screen, until they are overwritten.

    Usually HttpHeaders sits in a chain under a Storage holding the token — see Chain .

    Differences between the app and the site

    • On the site HttpData always runs in the browser , never while the page is built: a response to the build's request would be shown to every visitor. The page's HTML holds the id:loading slot (or the child with empty outputs), and a script fills in the data.
    • cache:true on the site keeps responses in the browser's localStorage , with the same lifetime — a day.
    • HttpHeaders on the site draws nothing of its own: its headers go out with every HttpData request of the page; a reactive value is updated in the browser.
    • A request from the browser is subject to CORS: the server must allow requests from the site's address.

    See also

    • Data generators — JsonData , FilterData , MapValue
    • Session — sign-in, tokens, cache reset
    • State — Storage for the token
    • ListView — ListView.builder for a list response