form_draft
Never lose what the user typed. form_draft saves Flutter form values while
the user is typing and restores them the next time the form opens — even after
the operating system killed the app in the background.
The problem
A user opens a long form, types for two minutes, switches apps to check an
email, and comes back to an empty form. Android has killed the process; iOS did
the same. Building this by hand means wiring TextEditingControllers,
serialization, lifecycle observers, debouncing and storage into every single
form — so most teams never do it.
form_draft does it once, with a small API that works with the Form you
already have.
What it does
- Saves values while typing, with a configurable debounce.
- Flushes the pending write when the app goes to the background.
- Restores values before the fields are built, so there is no cursor jump.
- Expires old drafts, and discards drafts written for an older form layout.
- Supports text fields, and any other value through a generic builder (switches, dropdowns, sliders, dates…).
- Lets you exclude sensitive fields — passwords, OTPs, card numbers.
- Has no platform channel and no plugin dependency: 39 tests run on any machine with Flutter, no emulator required.
Install
dependencies:
form_draft: ^0.1.0
Quick start
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:form_draft/form_draft.dart';
import 'package:form_draft/form_draft_io.dart';
final controller = DraftController(
id: 'contact', // stable, unique per form
store: FileDraftStore(directory),
maxAge: const Duration(days: 7),
);
DraftForm(
controller: controller,
builder: (context) => Column(
children: [
DraftTextFormField(
draftKey: 'name',
decoration: const InputDecoration(labelText: 'Name'),
),
DraftTextFormField(
draftKey: 'phone',
keyboardType: TextInputType.phone,
decoration: const InputDecoration(labelText: 'Phone'),
),
DraftTextFormField(
draftKey: 'password',
draftable: false, // never stored
obscureText: true,
),
],
),
);
That is all. The draft is written as the user types and restored when the page is opened again.
Where to store drafts
FileDraftStore writes one JSON file per form and creates its directory on
first use. On a real device, get the directory with path_provider:
final support = await getApplicationSupportDirectory();
final store = FileDraftStore(Directory('${support.path}/drafts'));
The core package is platform independent. Implement DraftStore to use any
backend you prefer:
class PreferencesDraftStore implements DraftStore {
// read / write / delete, each returns a Future
}
InMemoryDraftStore is included for tests and previews.
API
| Widget / class | Purpose |
|---|---|
DraftController |
Owns one draft: values, restore, debounced writes, expiry. |
DraftForm |
Wraps a Form, restores before building and flushes on background. |
DraftTextFormField |
A TextFormField whose text is saved under draftKey. |
DraftValueBuilder<T> |
Saves any JSON-compatible value — switch, dropdown, date. |
DraftSnapshot |
The persisted data; can be encoded and decoded on its own. |
DraftStore |
Storage interface. |
FileDraftStore |
File based store for dart:io platforms. |
InMemoryDraftStore |
Volatile store for tests. |
Controller options
| Option | Default | Meaning |
|---|---|---|
id |
required | Stable identifier of the form. |
debounce |
500 ms | Delay after the last keystroke before writing. |
maxAge |
null |
Drafts older than this are deleted on restore. |
schemaVersion |
1 |
Bump it when stored fields change incompatibly. |
When the form was submitted
Delete the draft once the data is safe, and increment schemaVersion whenever
the set of fields changes:
await controller.discard();
Restoring with the user's permission
Set restoreOnStart: false, call await controller.restore() yourself, and
take the decision from the returned DraftSnapshot?:
final snapshot = await controller.restore();
if (snapshot != null && await _askUser('Restore your last draft?')) {
// Build the form with the restored controller.
}
Security
Drafts are stored as plain JSON. They are convenience data, not a secret
store. Set draftable: false on password, one-time-code and payment fields, and
use a secure store implementation if your drafts contain personal data.
Compatibility
The package supports Flutter 3.24 and newer and has no plugin dependency.
It imports package:flutter/material.dart, which Flutter 3.47 deprecated in
favour of package:material_ui; the import is kept so that existing apps on
older SDKs can use the package.
Example
See example/lib/main.dart for a complete contact
form, including a restore banner, validation, a "discard draft" action and a
field that is deliberately excluded from the draft.
cd example
flutter run
Contributing
Issues and pull requests are welcome at https://github.com/phantome001/form_draft.
flutter pub get
flutter analyze
flutter test
License
MIT — see LICENSE.
Libraries
- form_draft
- Automatic draft saving and restoring for Flutter forms.
- form_draft_io
- Draft stores that need
dart:io.