---
title: Data Types
description: HWDataType variants used to declare widget data fields.
---
> ## Documentation Index
> Fetch the complete documentation index at: https://docs.page/abausg/home_widget/llms.txt
> Use this file to discover all available pages before exploring further.

# Data Types

`HWDataType` describes a single data field stored for a widget. You declare data fields in two places:

- Implicitly, by binding a widget to data (e.g. `HWText(HWInt('count'))`).
- Explicitly, via [`HWDataOnly`](/generator/widgets/hw-data-only) or `HWConditional` for fields that aren't rendered directly.

Each variant carries a `key` (the storage key) and an optional `defaultValue`.

```dart
@HomeWidget(
  name: 'Counter',
  widget: HWText(HWInt('count', defaultValue: 0)),
  android: HomeWidgetAndroidConfiguration(packageName: 'com.example.app'),
  iOS: HomeWidgetIOSConfiguration(groupId: 'group.com.example.app'),
)
class CounterWidget {}
```

The generated Dart helper exposes typed setters for each declared field, e.g. `setCount(42)` / `update(count: 42)`.

## Variants

| Variant     | Constructor                                | Dart type             | Description                                                                                  |
| ----------- | ------------------------------------------ | --------------------- | -------------------------------------------------------------------------------------------- |
| `HWString`  | `HWString(key, {defaultValue, previewValue})` | `String`            | Stored as a string in `SharedPreferences` / `UserDefaults`.                                  |
| `HWString.localized` | `HWString.localized(key, {required defaultTranslations, previewTranslations})` | generated translations class | Per-locale text. `defaultTranslations` maps locale tag to the text shipped with the widget; your app can override any locale at runtime. |
| `HWInt`     | `HWInt(key, {defaultValue, previewValue})` | `int`                 | Integer value.                                                                               |
| `HWDouble`  | `HWDouble(key, {defaultValue, previewValue})` | `double`            | Double / float value.                                                                        |
| `HWBool`    | `HWBool(key, {defaultValue, previewValue})` | `bool`               | Boolean value.                                                                               |
| `HWDateTime`| `HWDateTime(key, {previewValue})`          | `DateTime`            | A point in time, stored as an ISO-8601 UTC string. Rendered in the device's locale and time zone — see [Formatting](/generator/formatting). |
| `HWJson`    | `HWJson(key, child)`                       | `Map<String, dynamic>`| Stored as a JSON string. Wraps another `HWDataType` to address a nested leaf (e.g. `HWJson('user', HWString('name'))`). |
| `HWImageData` | `HWImageData(key, {previewAsset})` / `HWImageData.asset(path, {package})` | `ImageProvider`  | An image, saved at runtime or bundled as a Flutter asset. See [Image data](#image-data) below. |
| `HWIconData` | `HWIconData(key, {required icons, defaultValue, previewValue})` | generated icon enum | An icon chosen from a fixed set, rendered with [`HWIcon`](/generator/widgets/hw-icon). See [Icon data](#icon-data) below. |
| `HWItemData` | `HWItemData(data, {previewValues})` | the wrapped type's, on the generated item class | Wraps another type to read a field of the list item a builder is rendering, taking its key and the applicable default and preview. See [List items](#list-items) below. |

`HWString.localized` requires a `localization:` block on the annotation. Its generated setter takes a class with one required field per supported locale, and the widget picks the right one at render time. See [Localization](/generator/localization).

`HWInt` is typed `int` in Dart, `Int` in the generated Swift and `Long` in the generated Kotlin — a Dart `int` is 64-bit, so the Android side reads it as a `Long` rather than truncating it to an `Int`.

`HWDateTime` is typed `DateTime` in Dart, `Date` in the generated Swift and `java.util.Date` in the generated Kotlin. Storage is a plain string: `saveData` writes `value.toUtc().toIso8601String()`, and `getData()` parses it back into a **UTC** `DateTime` — call `.toLocal()` if you want the local one. The widget always displays it in the device's current time zone. See [Dates](/generator/formatting#dates).

## Defaults

When a `defaultValue` is provided it's used both in the generated Dart helper and on the native side when the key is missing from storage. Without a default, the read path returns `null` (or the language's optional equivalent).

`HWImageData` and `HWDateTime` are the exceptions: neither supports a `defaultValue`. A missing image simply renders nothing — see [`HWImage`](/generator/widgets/hw-image) for a placeholder pattern using `HWDataExists`. A missing date renders as empty text, and the same `HWDataExists` pattern gives it a placeholder.

A missing **number** follows the same rule: with no `defaultValue`, an `HWInt` or `HWDouble` bound to an `HWText` renders as empty text rather than as a formatted `0` — see [Missing values](/generator/formatting#missing-values).

`HWString`, `HWInt`, `HWDouble`, `HWBool`, `HWDateTime` and `HWIconData` also take a `previewValue` next to their default; `HWImageData` takes a `previewAsset` and `HWString.localized` takes `previewTranslations` instead. `HWJson` and `HWTimedData` declare none of their own — they preview through the type they wrap or the leaf they address. Each of these is a sample shown only in the widget gallery, never on the home screen. See [Previews](/generator/previews).

## Image data

`HWImageData` describes an image slot rendered with [`HWImage`](/generator/widgets/hw-image). Supply runtime images through the generated `saveData(...)`, which takes an `ImageProvider` for each of them. `getData()` hands a top-level image back as the `String?` file path `saveData` wrote it to; only images nested in an `HWJson` group or in a timed entry come back from `getData()` as an `ImageProvider?`.

There are two constructors, matching the two `HWImage` constructors:

- `HWImageData('key')` — a **runtime** image. Your app supplies it by passing an `ImageProvider` to the generated `saveData`.
- `HWImageData.asset('assets/logo.png')` — a **bundled** image. The native widget reads the Flutter asset straight out of the installed app bundle when it renders, so there is nothing to call at runtime and the image is there before the app has ever been launched. Asset images get no `saveData`, `deleteData` or `getData` entry. Pass `package:` to use an asset from a dependency: `HWImageData.asset('assets/logo.png', package: 'my_icons')`, mirroring `Image.asset`.

An asset image takes no key: two asset images in one widget that would be indistinguishable are reported as a generation error, and combining `package:` with a manual `packages/<package>/` path is one too.

Asset paths are checked when you run `home_widget generate`: app assets must exist and be declared under `flutter.assets`, and package assets must exist inside the resolved dependency — package assets are only checked once the dependency is resolved in `.dart_tool/package_config.json` (i.e. after `pub get`); if it isn't resolved yet, the check is skipped.

Binding an `HWImageData` to a text-only widget (e.g. `HWText(HWImageData('avatar'))`) is a generation error — an image is not display text. Use `HWImage` to render it.

A runtime image can also be wrapped in `HWTimedData` to swap pictures on a schedule — `HWImage(HWTimedData(HWImageData('slide')))`, see [Time-based images](/generator/timed-data#images) — or sit at the leaf of an `HWJson` group so a picture travels with the rest of a record: `HWImage(HWJson('contact', HWImageData('avatar')))`, see [Images at a JSON leaf](#images-at-a-json-leaf). Only `HWImageData.asset` is rejected inside either wrapper: an asset ships with the app, so there is nothing to store and nothing to vary.

## Icon data

`HWIconData` describes an icon chosen from a fixed set, rendered with
[`HWIcon`](/generator/widgets/hw-icon):

```dart
HWIcon(
  HWIconData(
    'mood',
    icons: [Icons.wb_sunny, Icons.cloud, Icons.umbrella],
    defaultValue: Icons.wb_sunny,
    previewValue: Icons.cloud,
  ),
)
```

- `key` — the storage key, like any other data type.
- `icons` — required, non-empty list of `IconData` constants the field can ever hold. Every icon
  must come from the same icon font family (`Icons.*` and `CupertinoIcons.*` cannot be mixed in one
  list) — a generation error names the field and the offending icon otherwise. No two of them may be
  the same glyph either: Flutter's aliases (`Icons.airplanemode_active` and
  `Icons.airplanemode_on` are both `0xe06e`) store identically, so listing both is an error.
- `defaultValue` / `previewValue` — optional `IconData` constants, and must each be a member of
  `icons` when given.

For each `HWIconData` field, the generated Dart helper emits an enum named
`<WidgetClassName><FieldKeyPascal>Icon` with one value per listed icon, each exposing an `icon`
getter that returns the original `IconData`. `saveData(...)` takes this enum rather than a raw
`IconData`, so only an icon actually declared in `icons` can ever be saved — see
[Icons](/generator/icons#runtime-icons) for the full generated shape and an example. The name is
built from the field key alone, so two fields sharing a key — one `icon` under `HWJson('today', ...)`
and another under `HWJson('tomorrow', ...)` — share one enum holding the union of both lists.

An `HWIconData` value is stored as the icon's Unicode code point. A field with no stored value and
no `defaultValue` renders nothing, exactly like a missing `HWImageData`. It works nested inside
`HWJson` and `HWTimedData`, the same as any other data type.

## Nested JSON

`HWJson` lets you read a single leaf out of a JSON blob shared via `home_widget`. Chain `HWJson` to address deeper paths:

```dart
HWText(HWJson('user', HWJson('profile', HWString('name'))))
```

This reads `user.profile.name` from a JSON string stored under the `user` key.

Every segment of the path becomes a member of a generated `*JsonData` class, so besides the rules every data key follows it cannot be named `toJson`, `toString`, `hashCode`, `runtimeType` or `noSuchMethod` — the class already carries those. `fromJson` is free, since the class names a constructor rather than a member with it.

A leaf may also be an `HWString.localized`. The value found at the JSON path wins; when the path yields nothing, the leaf's `defaultTranslations` are resolved against the device locale instead — see [Localization](/generator/localization).

An `HWDateTime` leaf becomes a `DateTime` field on the generated `*JsonData` class and is stored as the same ISO-8601 UTC string as a top-level date field — see [Timed and JSON values](/generator/formatting#timed-and-json-values).

### Images at a JSON leaf

A leaf may be an `HWImageData`, so a picture travels with the rest of a record instead of needing a field of its own:

```dart
HWColumn(
  children: [
    HWText(HWJson('contact', HWString('name'))),
    HWImage(HWJson('contact', HWImageData('avatar'))),
  ],
)
```

The generated `ContactJsonData` then carries `final String? name` next to `final ImageProvider? avatar`, and one `saveData(contact: ...)` call persists both. Saving the group with a null image clears that picture, and `deleteData(contact: true)` removes the group together with every image it owns. See [`HWImage`](/generator/widgets/hw-image#images-inside-a-json-group) for the full contract.

`HWImageData.asset` is not allowed at a JSON leaf: an asset is read from the app bundle and never stored, so there is nothing for the blob to carry.

## List items

`HWItemData` reads a field of the item an `HWColumn.builder` or `HWRow.builder` is rendering, rather than a field of the widget's own data:

```dart
HWRow.builder(
  'forecast',
  maxItems: 5,
  item: HWText.number(HWItemData(HWInt('temperature', defaultValue: 0))),
)
```

It wraps an `HWString` (plain or localized), `HWInt`, `HWDouble`, `HWBool`, `HWDateTime`, `HWIconData` or runtime `HWImageData`, and takes its key from the type it wraps, along with the applicable default and preview — `defaultValue`/`previewValue` for most, `defaultTranslations`/`previewTranslations` for `HWString.localized`, and `previewAsset` with no default for a runtime `HWImageData`; see [`HWItemData`](/generator/lists#hwitemdata) for the full mapping. `previewValues` varies the sample from one gallery item to the next. The app saves the whole list through the generated helper, e.g. `saveData(forecast: [WeatherForecastItem(temperature: 21)])`. See [Lists](/generator/lists) for the generated item class, defaults, previews and the Android limits.
