Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Magic Framework?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/anilcancakir-magic-framework)
---
name: magic-framework
description: "Write idiomatic Flutter code on the magic framework: facades, models, controllers, views, routing, forms, auth and tests."
version: 0.3.3
when_to_use: "Use when code imports package:magic or touches Magic.init, a facade, a model, a controller, a provider or a make:* command."
---
<!-- magic 1.4.0 | Skill v0.3.3 (2026-12-03). API surface verified against lib/src. -->
# Magic Framework
The full-stack framework for Flutter: Eloquent-style models, auth, routing, realtime and payments, wired and ready on every device. The core package carries the IoC container, facades, an Eloquent-style ORM over REST, service providers, reactive controllers, GoRouter-backed routing, auth and validation; realtime, payments and the rest arrive as official plugins. This skill makes an agent write what an experienced magic developer would write: facade-first, IoC-resolved, reactive, and checked against the real API in `lib/src`. The vocabulary will feel familiar to Laravel developers; section 3 maps it and names where it diverges.
Scope: the core `magic` package only. Every official plugin (broadcasting, SQLite, encryption, picking, payments, notifications, deep links, social login, the starter kit, Sentry, device gates, devtools) ships its own skill: section 10 says what each plugin is and which skill to load. Visual styling is Wind (load `wind-ui`).
## Install
An app that does not depend on magic yet adds it and scaffolds the structure:
```bash
flutter pub add magic
dart run magic:dowel magic:install
```
`magic:install` adds `wind_kit`, `material_ui` and `dowel` to `pubspec.yaml` and writes `lib/config/`, the `AppServiceProvider` and `RouteServiceProvider`, `lib/app/kernel.dart`, `lib/routes/app.dart`, a welcome view, `.env`, `.env.example` and a `lib/main.dart` that awaits `Magic.init()`. `--without-auth`, `--without-network`, `--without-cache`, `--without-localization` and `--without-logging` skip a layer; `--with-devtools` wires the debug tooling. Core only: a plugin is `flutter pub add <package>` then `dart run magic:dowel plugin:install <package>` (section 10).
The host app then depends on `package:magic/magic.dart`. Every API you write must exist in `lib/src`; when unsure of a signature, open the source or the matching `doc/**` page instead of guessing. A `doc/<section>/<page>.md` path in this skill is the page `https://anilcancakir.com/magic/docs/<section>/<page>` (add `.md` for the raw text).
## 0. Before writing code in this project
1. **Read `lib/main.dart` and `lib/config/app.dart`.** Note the `providers` list and its order (`AppServiceProvider` must precede `AuthServiceProvider` so `setUserFactory` is set before auth restore runs), and whether `configFactories` or `configs` is used.
2. **Scan one existing controller and view pair in `lib/app/`** for the project's idioms: the singleton accessor, how views resolve controllers, how forms are wired. Match the surrounding code, do not invent a dialect.
3. **Run the CLI as `dart run magic:dowel <cmd>`.** Magic ships a `dowel` executable, so there is no package-name placeholder and no global activation.
## 1. Core laws
1. **`await Magic.init()` first.** Await it in `main()` before any facade call and before `runApp()`. Never `.then()`: providers are not booted until the future completes.
2. **Facade-first.** Reach for `Auth`, `Http`, `Config`, `Cache`, `Log`, `Event`, `Lang`, `MagicRoute`, `Gate`, `Session`, `Vault`, `Storage` and `Launch`. `DB`, `Schema`, `Echo`, `Pick` and `Crypt` belong to plugins (section 10). Resolve from the container by hand (`Magic.make<T>('key')`) only when extending the framework.
3. **Controllers are singletons.** `static X get instance => Magic.findOrPut(X.new);` is the accessor. Views resolve controllers with `Magic.find<T>()` (automatic in `MagicView`), never through a constructor. `Magic.find` throws when nothing registered the controller, so register it before the view's first build: reading `X.instance` or calling `Magic.put` does it, while declaring the getter alone does not. Registering does not run `onInit()`: the first `MagicStatefulView` that mounts the controller runs it, a `MagicView` never does (a `SimpleMagicController` runs it from its constructor).
4. **IoC over `new` for services.** Bind in a provider's `register()`, resolve through the facade or `Magic.make<T>('key')`; do not scatter `Service()` construction across the app.
5. **Provider discipline.** `register()` is synchronous and only binds services, the middleware kernel and event listeners. `boot()` is async and may resolve other services: routes go there (`RouteServiceProvider.boot()` calls `registerAppRoutes()`), and so does `Auth.manager.setUserFactory(...)`.
6. **Reactive state, not `setState`.** Controllers extend `MagicController` (a `ChangeNotifier`); state flows through `MagicStateMixin` and `RxStatus`. Use `refreshUI()` (the guarded `notifyListeners` that every controller notification goes through), `setLoading/setSuccess/setError/setEmpty`, `MagicBuilder` for a section backed by a `ValueListenable`, and `MagicSelector` for a section backed by a plain controller field (it caches its child, so it survives the parent's `setState`). `MagicPerfHooks.sink` is a null-by-default static that tooling sets to observe those notifications; there is no `onRefreshUI`. Local `setState` belongs only to widget-local UI state inside a `MagicStatefulView`.
7. **Typed attribute access.** A model declares a typed getter and setter per field, and code outside the model reads those, not a raw `getAttribute('key')`. Inside the model, `get<T>('key')` (what `make:model` generates) returns `null` on a type mismatch, while `getAttribute('key') as T?` (what the `doc/` pages show) throws; write with `set('key', v)`. Declare `fillable`, and call `fill(validated, strict: true)` after validation so schema drift throws `MassAssignmentException`; a request that also validates a field the model does not store (a password) sends it in the request body instead (section 5, FormRequest).
8. **Context-free navigation and feedback.** `MagicRoute.to/back/replace` and `Magic.snackbar/toast/dialog/confirm/loading`. Never depend on a `BuildContext` for them, and never navigate or fetch inside `build()`.
9. **Validate at the boundary.** `MagicFormData` for forms, `FormRequest` for complex payloads, `Validator` for ad hoc checks. Surface server errors with `handleApiError(response)` from the `ValidatesRequests` mixin.
10. **Trailing commas and multi-line collections, always.** Match the project's style.
## 2. Bootstrap
```dart
import 'package:magic/magic.dart';
import 'config/app.dart'; // the app's own config files declare appConfig, authConfig, networkConfig
import 'config/auth.dart';
import 'config/network.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Magic.init(
configFactories: [
() => appConfig, // factories run after Env.load(), so env() works inside them
() => authConfig,
() => networkConfig,
],
);
runApp(const MagicApplication(title: 'My App'));
}
```
Lifecycle (`lib/src/foundation/magic.dart`): `Env.load()`, then `configFactories` evaluate, then `MagicApp.init` (config merge), then the web URL strategy when `routing.url_strategy == 'path'`, then core bindings, then providers `register()` (sync), then `await boot()` (async), then the router pre-builds, then ready.
Use `configFactories` (not `configs`) whenever a config value reads `Env.get()`: `configs` is evaluated before Env is loaded. `MagicApplication` accepts `title`, `titleSuffix`, `windTheme`, `themeMode`, `locale`, `localizationsDelegates`, `onThemeChanged`, `onInit`, `initialRoute` and `builder`, which wraps the router like `MaterialApp.builder` so a layer above every page (a floating player) survives each navigation.
## 3. Mental model: Laravel to magic, and where it diverges
Magic mirrors Laravel's vocabulary and diverges wherever Dart lacks PHP's reflection or the target is a Flutter client, not an HTTP server. Most divergences fail silently (null, not an exception).
| Laravel | magic | Note |
|---|---|---|
| `Container` autowiring, `__callStatic` facades | string-keyed factory closures and explicit static facade stubs | no reflection and no autowiring; an unregistered key throws at runtime |
| `ServiceProvider::boot()` (sync) | `boot()` is `async` | await it; a dropped future leaves a half-booted provider |
| `Router::resource` returns Responses | routes resolve to widgets; middleware runs on navigation | not an HTTP request cycle |
| controllers are per-request handlers | controllers are reactive `ChangeNotifier` singletons | they live for the session and drive the UI through `RxStatus` |
| Eloquent lazy load and `with()` | relations are cast from nested API Maps, cached on first access | no lazy load, no `with()`: if the payload did not nest it, it is null |
| `Gate` and `Policy` are server-authoritative | they run client-side and are advisory | always re-authorize on the backend |
| server sessions | tokens in `Vault` (secure storage) with a cache-first restore | `Auth.restore()` on cold start |
The assumptions a Laravel developer gets wrong most: the container autowires (it does not, register explicitly); `user.posts` lazy-loads (it does not, embed it in the payload); `Gate.allows()` is security (it is advisory); `with()` exists (it does not); `boot()` is sync (it is async). Bootstrap and container details: `${CLAUDE_SKILL_DIR}/references/bootstrap-lifecycle.md`.
## 4. Facades and the container
| Call | Purpose |
|---|---|
| `Magic.bind('key', () => Svc(), {shared})` | factory binding (a new instance per resolve; `shared: true` caches) |
| `Magic.singleton('key', () => Svc())` | lazy shared singleton |
| `Magic.make<T>('key')` | resolve a service (throws if unbound) |
| `Magic.bound('key')` | is the key registered |
| `Magic.put<T>(ctrl)` / `Magic.find<T>()` / `Magic.findOrPut<T>(T.new)` | controller register, resolve, get-or-create |
| `Magic.delete<T>()` / `Magic.isRegistered<T>()` | controller remove, check |
| `Magic.flush()` / `MagicApp.reset()` | clear controllers, full container reset (testing) |
`Config` and `Gate` resolve through their managers (no plain IoC key); the rest bind to the key shown.
| Facade | Key | Surface you reach for (verified in `lib/src/facades/`) |
|---|---|---|
| `Auth` | `auth` | `login(data, user)`, `logout()`, `check()`, `guest` (getter), `user<T>()`, `id()`, `getToken()`, `refreshToken()`, `restore()`, `registerModel<T>(factory)`, `guard([name])`, `stateNotifier`, `manager`, `fake({user})` |
| `Http` | `network` | `get/post/put/delete`, `upload`, RESTful `index/show/store/update/destroy`, `fake([stubs])`, `response([data, code])`, `unfake()`. No `patch` |
| `Config` | (manager) | `get<T>`, `getOrFail<T>`, `set`, `has`, `all`, `merge`, `prepend`, `push`, `forget`, `flush`, `repository` |
| `Cache` | `cache` | `put(key, value, {ttl})`, `get`, `has`, `forget`, `flush`, `remember<T>(key, ttl, cb)`, `fake()` |
| `Log` | `log` | `info/error/warning/debug/notice/critical/alert/emergency`, `log(level, msg)`, `channel(name)`, `fake()` |
| `Event` | (dispatcher) | `dispatch(MagicEvent)`; `listen<T extends MagicEvent>(() => Listener())` (name `T`, register in `register()` not `boot()`); or `EventDispatcher.instance.register(Type, [() => Listener()])`; an event that implements `ReportsBreadcrumb` is recorded as a breadcrumb by `magic_sentry` |
| `MagicRoute` | (router) | `page`, `group`, `layout`, `resource(name, ctrl, {only, except})`, `to`, `toNamed`, `push`, `back({fallback})`, `replace`, `setTitle`, `currentTitle`, `config` |
| `Gate` | (manager) | `define`, `before`, `allows`, `denies`, `allowsAny(list)`, `allowsAll(list)`, `has`, `abilities`, `flush` |
| `Session` | (store) | `flash(map)`, `flashErrors(map)`, `old(field, [fallback])`, `oldRaw`, `error(field)`, `errors(field)`, `hasError`, `hasFlash`, `tick()` |
| `Lang` | (translator) | `get(key, [replace])`, `has`, `current`, `isLoaded`, `supportedLocales`, `setLocale`, `detectLocale`, `detectAndSetLocale`, `setSupportedLocales`, `addSource(package, loader)`, `addListener/removeListener`, `delegate` |
| `Vault` | `vault` | `put(key, value)`, `get`, `delete`, `flush`, `fake([initial])` |
| `Storage` | (manager) | `disk([name])`, `put`, `get`, `getFile`, `exists`, `delete`, `url`, `download` (on IO it needs `magic_picker`, else `UnsupportedError`; web needs nothing), `setManager`, `flush` |
| `Launch` | `launch` | `url(u, {mode})`, `email`, `phone`, `sms`, `canLaunch` |
Global helpers are idiomatic: `env<T>(key, [default])`, `trans(key, [replace])`, `transChoice(key, count, [replace])`, `old(field, [fallback])`, `error(field)`, `carbonNow()`, `carbonToday()`, `carbonParse(s)`. Per-facade signatures: `${CLAUDE_SKILL_DIR}/references/facades-api.md`.
## 5. Canonical patterns
Annotated templates for each pattern: `${CLAUDE_SKILL_DIR}/references/templates.md`.
### Model
```dart
// UserStatus, UserTag (enums) and Company (a Model) are the app's own.
class User extends Model with HasTimestamps, InteractsWithPersistence, Authenticatable { // Authenticatable: only the model you pass to Auth.login or Auth.fake
@override String get table => 'users';
@override String get resource => 'users';
@override List<String> get fillable => ['name', 'email'];
@override bool get useLocal => true; // opt in to SQLite (needs magic_local_storage); the default is API-only
@override Map<String, dynamic> get casts => const {
'created_at': 'datetime', // Carbon
'settings': 'json', // Map or List
'status': EnumCast(UserStatus.values), // class-based cast
'tags': ListCast(EnumCast(UserTag.values)), // element-wise list cast
};
@override Map<String, Model Function()> get relations => {'company': Company.new};
@override int? get id => get<int>('id'); // narrows Model's dynamic id
String? get name => get<String>('name');
set name(String? v) => set('name', v);
Company? get company => getRelation<Company>('company'); // from the nested payload Map, not a query
static User fromMap(Map<String, dynamic> map) =>
User()..setRawAttributes(map, sync: true)..exists = true;
static Future<User?> find(dynamic id) =>
InteractsWithPersistence.findById<User>(id, User.new);
static Future<List<User>> all() =>
InteractsWithPersistence.allModels<User>(User.new);
}
```
Casts: `datetime`, `json`, `bool`, `int`, `double` (string keys), plus class-based `CastsAttributes<T>` (`EnumCast(values, {strict})`, `ListCast(inner)`). `save()` is API-first and then syncs to the local store when `useLocal` is true; core resolves that store from the `LocalModelStore` contract (container key `'local_model_store'`, bound by `magic_local_storage`), and a `useLocal` model with nothing bound throws a `StateError` naming the plugin. Relations are decoded from nested API Maps and cached on first access.
### Controller and reactive state
```dart
class UserController extends MagicController
with MagicStateMixin<List<User>>, ValidatesRequests {
static UserController get instance => Magic.findOrPut(UserController.new);
@override void onInit() { super.onInit(); load(); }
Future<void> load() => fetchList('/users', User.fromMap); // loading, success, error, empty
Future<void> store(Map<String, dynamic> data) async {
authorize('create-user'); // throws AuthorizationException if Gate denies
clearErrors();
final response = await Http.post('/users', data: data);
if (response.successful) { Magic.toast(trans('users.created')); MagicRoute.to('/users'); return; }
handleApiError(response, fallback: trans('users.create_failed')); // 422 becomes field errors
}
}
```
`fetchList<E>(url, fromMap, {dataKey: 'data', query, headers})` and `fetchOne(...)` drive the `RxStatus` transitions. In the view, `controller.renderState((data) => ..., onLoading: ..., onError: (msg) => ..., onEmpty: ...)` renders per state. `MagicResponse` exposes `.data` (the payload, never `.body`), `.successful`, `.failed`, `.errors` (parses Laravel `{"errors": {field: [..]}}`), `.firstError` and `.dataAs<T>()`.
### Views
```dart
// Stateful: its state runs the controller's onInit() once per controller (so load() fires) and listens to it.
// The type parameters run <Controller, View>.
class UserListView extends MagicStatefulView<UserController> {
const UserListView({super.key});
@override State<UserListView> createState() => _UserListViewState();
}
class _UserListViewState extends MagicStatefulViewState<UserController, UserListView> {
@override Widget build(BuildContext context) => controller.renderState(
(users) => ListView(children: [for (final u in users) Text(u.name ?? '')]),
onLoading: const Center(child: CircularProgressIndicator()),
onError: (msg) => Center(child: Text(msg)),
);
}
// MagicView is stateless: it resolves its controller with Magic.find<T>(), runs no onInit() and does not listen.
// Use it over a controller something else already loaded; renderState listens on its own, and MagicBuilder or
// MagicSelector cover other controller fields.
// Stateful with a form: TextEditingController, animations, onClose() for cleanup.
class LoginView extends MagicStatefulView<AuthController> {
const LoginView({super.key});
@override State<LoginView> createState() => _LoginViewState();
}
class _LoginViewState extends MagicStatefulViewState<AuthController, LoginView> {
late final form = MagicFormData({'email': '', 'password': ''}, controller: controller);
@override void onClose() => form.dispose(); // always dispose
void _submit() => form.process(() => controller.login(form.data));
@override Widget build(BuildContext context) => MagicForm(
formData: form,
child: WDiv(className: 'flex flex-col gap-4', children: [
WFormInput(controller: form['email'], type: InputType.email, label: 'Email',
validator: rules([Required(), Email()], field: 'email')),
WFormInput(controller: form['password'], type: InputType.password, label: 'Password',
validator: rules([Required()], field: 'password')),
MagicBuilder<bool>( // rebuilds the button when process() toggles the flag
listenable: form.processingListenable,
builder: (busy) => WButton(onTap: busy ? null : _submit, isLoading: busy, child: const WText('Sign in')),
),
]),
);
}
// Responsive (AuthController, DashboardController and the dashboard widgets are the app's own)
class DashboardView extends MagicResponsiveView<DashboardController> {
const DashboardView({super.key});
@override Widget phone(BuildContext context) => const MobileDashboard();
@override Widget tablet(BuildContext context) => const TabletDashboard();
@override Widget desktop(BuildContext context) => const DesktopDashboard();
}
```
`MagicResponsiveViewExtended` adds `xs/sm/md/lg/xl/xxl`. `MagicCan(ability: 'edit-post', arguments: post, child: ..., placeholder: ...)` and `MagicCannot` gate widgets on `Gate`.
### MagicFormData
```dart
final form = MagicFormData({
'email': '', // String -> TextEditingController
'accept_terms': false, // non-String -> ValueNotifier<T>
}, controller: controller);
form['email'] // TextEditingController
form.get('email') // trimmed String
form.value<bool>('accept_terms') // read a ValueNotifier
form.setValue('accept_terms', true) // write a ValueNotifier
form.data // Map<String, dynamic> of all fields
form.validate() // bool; on failure it flashes form.data (input only) to Session
form.process(() => submit()) // toggles isProcessing; throws if already processing
form.dispose() // in onClose()
```
### FormRequest
```dart
class StoreUserRequest extends FormRequest {
@override bool authorize() => Gate.allows('create-user');
@override Map<String, dynamic> prepared(Map<String, dynamic> data) =>
{...data, 'email': (data['email'] as String?)?.trim().toLowerCase()};
@override Map<String, List<Rule>> rules() => {
'name': [Required()],
'email': [Required(), Email()], // an AsyncRule such as Unique passes here: run it with validateRequestAsync
};
}
final validated = StoreUserRequest().validate(form.data); // throws Authorization or ValidationException
final user = User()..fill(validated, strict: true); // every validated key is a model field
await user.save();
```
`validate()` returns exactly the keys `rules()` names, so `fill(validated, strict: true)` works when each of them is a model field, and throws `MassAssignmentException` when one leaves `fillable` (schema drift). A request that also carries a field the model does not store (a password the backend hashes) is not filled: `save()` sends only the model's attributes, so send the whole `validated` map as the request body with `Http.store('users', validated)` and hydrate the model from the response.
Override `attributes()` (`{'url': 'Server address'}`) to name a field for `:attribute` in this request's messages ahead of the global `attributes.<key>`.
Rules: `Required`, `Email`, `Min(n)`, `Max(n)`, `Confirmed`, `Same(other)`, `Accepted`, `In<T>(values)` (primitives), `InList<T extends Enum>(values, {caseInsensitive, wire})` (enums), `Unique(endpoint, {field, debounce})`. Async rules implement `AsyncRule.passesAsync` and run through `Validator.make(data, rules).validateAsync()`. `Unique` debounces (400 ms by default), passes on a network error and discards stale calls; swap the backend with `.via(resolver)`. More rules (`Numeric`, `Between`, `Regex`, `Date`, `Nullable`, `RequiredIf`, `ArrayRule` and others) are in `doc/digging-deeper/validation.md`.
A controller with `ValidatesRequests` calls `validateRequest` or `validateRequestAsync`, not `FormRequest.validate()` directly: the latter never touches `validationErrors`, so the controller's error bag, its repaint and the stale-error clear on resubmit are skipped. Both run the same authorize and prepare sequence and return the full prepared map, not the rule-filtered one; use `validateRequestAsync` when `rules()` contains an `AsyncRule`.
```dart
final payload = validateRequest(StoreUserRequest(), form.data); // throws; populates validationErrors
```
Mix `CollapsesIndexedErrorKeys` on top of `ValidatesRequests` to collapse a backend's indexed list-validation key (`items.0.name`) onto its field name (`name`) for a form with one error slot per field; `CollapsesIndexedErrorKeys.collapse(wireKey)` (static) does the same for a controller that cannot mix it in.
`BaseGuard.startSession` and `logout()` (and the fake guard of `Auth.fake()`) dispatch `AuthLogin` and `AuthLogout` through `Event`: `AuthLogin` at the end of a successful `startSession`, `AuthLogout` after the state bump on every `logout()` including a guest's, never on a restore (`AuthRestored` covers that, API-confirmed sync only).
### Actions, repositories and session scope
Three primitives divide a write-heavy screen the way Laravel divides a controller-heavy one. Pages: `doc/architecture/application-structure.md` (the map), `doc/basics/actions.md`, `doc/eloquent/repositories.md`, `doc/digging-deeper/session-scope.md`. Realtime updates into a controller come from `magic_socket` (`ListensToBroadcasts`, `BroadcastListeners`): load `magic-socket`.
```dart
// A write: stateless, resolved through MagicAction.resolve so a test can MagicAction.bind a fake.
class PauseMonitor extends MagicAction<String, void> {
const PauseMonitor();
@override Future<void> handle(String id) async { /* ... */ }
}
class MonitorController extends MagicController with RunsActions, OwnsTimers {
Future<void> pause(String id) async {
final outcome = await runAction(MagicAction.resolve(PauseMonitor.new), id, key: id);
if (!outcome.succeeded) return;
}
// Repository<T>: one row per id, shared by every RepositoryQuery built over it.
late final query = RepositoryQuery<Monitor>(repository: MonitorRepository.instance);
@override void onClose() { query.dispose(); super.onClose(); }
}
// SessionScope: attach last in boot(), after realtime and polling already point at the new identity.
SessionScope.identity = () => Auth.check() ? '${Auth.id()}:$teamId' : null;
SessionScope.attach();
```
`Repository` registers itself with `SessionScope` in its own constructor (never `Magic.put` or `findOrPut` it). Its write paths (`upsertFromList`, `upsertFromShow`, `patch`, `evict`) notify only when the cache actually changed, so a controller that must repaint after a read to clear its own loading flag calls its own `refreshUI()`. A controller implementing `SessionScoped` needs no registration: `SessionScope.sync()` finds it through `Magic.controllers.whereType<SessionScoped>()`. `OwnsTimers.own(cancellable)` accepts a `PollHandle`, `Countdown`, `Debouncer`, `Timer` or `StreamSubscription` and cancels each from `onClose`. `MagicFormObject` (one per `State`, disposed from `onClose`, never registered in the container) composes `MagicFormData`, `ValidatesRequests` and `RunsActions`; see `doc/basics/forms.md#magicformobject`.
### Support helpers
`Number`, `Str`, `Arr` and `Cast` (`lib/src/support/`) are static namespaces, with no facade or IoC binding:
```dart
Number.currency(1234.5, code: 'TRY', locale: 'tr'); // '₺1.234,50'
Str.upper('istanbul', locale: 'tr'); // 'İSTANBUL', dotted-i aware
Cast.intOr(Arr.get(payload, 'meta.priority'), 0); // Arr does no type check; compose with Cast
```
`RefetchesOnMount<Controller, View>` (mix onto a `MagicStatefulViewState`, point `refetch` at a controller load that joins an in-flight request) and `SubmitsOnce<W>` (mix onto a form's `State`, route the handler through `submitOnce`, feed `isSubmitting` to the button's `isLoading`) close two gaps: singleton controllers fire `onInit` once per instance, not per mount, and nothing disables a button while an async submit is pending. `Env.filled(key, fallback)` treats an absent, blank or quote-only `.env` value alike and returns `fallback`; `Env.get` and `env()` fall back only on a fully absent key, and `Env.getOrFail(key)` throws a `StateError` only when the key is absent entirely. `Carbon.shortDiffForHumans([other])` is the compact sibling of `diffForHumans()` (`'14m ago'`). More in `${CLAUDE_SKILL_DIR}/references/secondary-systems.md` and in `doc/digging-deeper/helpers.md` and `doc/digging-deeper/validation.md`.
### Routing and resources
```dart
// In RouteServiceProvider.boot() (boot() runs before the router pre-builds, so any route added after Magic.init throws)
MagicRoute.page('/dashboard', () => const DashboardPage()).title('Dashboard').middleware(['auth']);
MagicRoute.resource('users', UserRoutes()); // index, create, show, edit
MagicRoute.resource('posts', PostRoutes(), only: ['index', 'show']);
MagicRoute.page('/stock/add', () => AddItemSheet()).overlay(base: '/stock'); // URL-backed bottom sheet
```
`ResourceController` supplies `index()`, `create()`, `show(id)` and `edit(id)`; `resource()` wires `GET /name`, `/name/create`, `/name/:id` and `/name/:id/edit` with the names `{name}.{method}`. Middleware extends `MagicMiddleware` (`handle(next)`, call `next()` to allow) and is registered with `Kernel.register('name', () => Mw())` from a provider's `register()` or `boot()`, both of which run before the router pre-builds; an alias nothing registered throws a `StateError` out of `Magic.init` naming every offending route. Read path and query params with `Request.route('id')` and `Request.query('q')`. `Session.tick()` is not automatic: wire it once in `main()` after `Magic.init()` on a router-delegate listener gated to changes of the delegate's `currentConfiguration`, not `currentLocation`, which changes one notification late (`${CLAUDE_SKILL_DIR}/references/facades-api.md`).
## 6. Testing
```dart
import 'package:flutter_test/flutter_test.dart';
import 'package:magic/magic.dart'; // the facades: Auth, Http, Gate
import 'package:magic/testing.dart'; // a separate barrel: fakes and MagicTest
// plus the app's own imports for User and UserController
void main() {
MagicTest.init(); // registers setUpAll, setUp (MagicApp.reset and Magic.flush) and tearDown
test('store turns a 422 into field errors', () async {
final http = Http.fake({'users': Http.response({'errors': {'email': ['Taken.']}}, 422)});
Gate.define('create-user', (user, _) => true); // an undefined ability is denied, and MagicTest.init flushes the Gate
Auth.fake(user: User.fromMap({'id': 1}));
await UserController.instance.store({'name': 'A', 'email': 'a@b.co'});
expect(UserController.instance.hasError('email'), isTrue);
http.assertSent((request) => request.method == 'POST');
});
}
```
`MagicTest.init()` builds no router, so the success branch of `store()` (`MagicRoute.to`) throws `Router not initialized` in a plain `test`: cover it in a widget test that pumps the app's router, or keep navigation out of the controller method under test.
These facades fake without a mock library: `Http.fake` (`FakeNetworkDriver`: `assertSent/assertNotSent/assertSentCount/assertNothingSent`), `Auth.fake` (`assertLoggedIn/assertLoggedOut/assertLoginAttempted/assertLoginCount`), `Cache.fake` (`assertHas/assertMissing/assertPut`), `Vault.fake` (`assertWritten/assertDeleted/assertContains/assertMissing`) and `Log.fake` (`assertLogged/assertLoggedError/assertNothingLogged/assertLoggedCount`). A plugin's fakes ship with the plugin (`Echo.fake` in `package:magic_socket/testing.dart`): see its skill. A `useLocal` model test binds a `LocalModelStore` (an in-memory one by contract inheritance, or `magic_local_storage`'s). Every `setUp` must reset the container (`MagicTest.init()` does); skipping it leaks state and gives false passes. A widget test that needs a few real providers uses `MagicTest.bootProviders(providers)` (register all, then boot all, as `Magic.init` does) with `MagicTest.disposeBooted()` in `tearDown`. Patterns: `${CLAUDE_SKILL_DIR}/references/testing-patterns.md`.
## 7. Mistakes to avoid
| Wrong | Right | Why |
|---|---|---|
| `Magic.init().then(...)` | `await Magic.init()` | facades are unusable before providers boot |
| `new Service()` or `Magic.make` inline in a view | bind in a provider, use the facade | inline construction breaks IoC and test isolation |
| a plain `StatefulWidget` with `setState` | `MagicView` or `MagicStatefulView` | the container resolves the controller and `RxStatus` drives rebuilds |
| `with()` or a query to load a relation | embed the relation in the API payload, or fetch it and `set` it | no `with()` and no lazy load exist |
| assuming `useLocal` is on | override `useLocal => true` and install `magic_local_storage` | it defaults to false; a `useLocal` model without the plugin throws a `StateError` |
| trusting `Gate.allows()` as authorization | re-authorize on the backend | the client-side Gate is advisory |
| `user.getAttribute('name')` outside the model | a typed getter (`user.name`) built on `get<String>('name')` | the model owns the key and the type |
| controller passed through a view constructor | `Magic.find<T>()` or `MagicView` | that is how magic resolves controllers |
| waiting for flash to expire after navigation | wire `Session.tick()` once at bootstrap | it is not automatic, so old input lingers |
| `Http.patch(...)` | `Http.put(...)` or `Http.update(...)` where the endpoint accepts PUT; for a Laravel endpoint that takes only PATCH, `Http.post(url, data: {..., '_method': 'PATCH'})` | there is no `patch` verb, and PUT is not a general substitute |
| `response.body` | `response.data` | `.body` does not exist on `MagicResponse` |
| `configs: [appConfig]` that reads `env()` | `configFactories: [() => appConfig]` | Env is not loaded when `configs` evaluates |
| `Auth.guest()` | `Auth.guest` | it is a bool getter |
| `Http.get()` or `MagicRoute.to()` in `build()` | call it from `onInit()` or a callback | no I/O or navigation during build |
| `user.fill(unvalidated)` | `user.fill(validated, strict: true)` | catches schema drift after validation; a non-model field (a password) goes in a request body, not `fill` |
| `if (!Gate.allows(...)) throw` by hand | `authorize('ability')` in the controller | delegates to Gate and throws `AuthorizationException` |
| a `Pick`, `Crypt`, `Echo`, `DB` or `Schema` call with no plugin | add the plugin, register its provider, load its skill | core has none of these facades |
| `Storage.download` on mobile or desktop throwing `UnsupportedError` | register `magic_picker`'s `PickerServiceProvider` | it sets `LocalAdapterContract.downloadHandler`; web needs nothing |
| four `MagicRoute.page()` calls for CRUD | `MagicRoute.resource(name, ctrl)` | wires the canonical routes and titles |
| skipping the reset in tests | `MagicTest.init()` (or `MagicApp.reset()` and `Magic.flush()` in `setUp`) | leaked state and false passes |
## 8. Pre-completion checklist
Before reporting a magic task done, verify with evidence:
- [ ] `dart analyze` on the changed files: zero issues. `dart format .` produces no diff.
- [ ] Imports use `package:magic/magic.dart` (and `package:magic/testing.dart` in tests).
- [ ] Every facade method and signature you wrote exists in `lib/src` (you opened the source or the `doc/**` page when unsure).
- [ ] Controllers have the `static X get instance => Magic.findOrPut(X.new)` accessor; views extend the right base; `MagicStatefulViewState<Controller, View>` is in that order.
- [ ] `MagicFormData` is disposed in `onClose()`; `ValidatesRequests` comes from `package:magic/magic.dart`.
- [ ] Routes are registered in a provider's `boot()` and have `.title(...)`; `configFactories` is used when values read `env()`.
- [ ] Tests reset the container in `setUp`; the post-change sync in the project's `CLAUDE.md` (CHANGELOG, `doc/`, skill, example) is honoured for `lib/` changes.
- [ ] A `Repository` is a plain singleton, never registered through `Magic.put` or `findOrPut`; a `MagicFormObject` is created per `State` and disposed from `onClose`.
- [ ] `SessionScope.attach()` runs last in `boot()`, after realtime and polling already point at the session.
## 9. CLI
The CLI runs on dowel and ships as the `dowel` executable in magic's `pubspec.yaml` (`executables: { dowel: }`, backed by `bin/dowel.dart`). With magic as a dependency, run any command as `dart run magic:dowel <cmd>`; nothing needs a global activation.
```bash
dart run magic:dowel magic:install # scaffold the project structure
dart run magic:dowel magic:install --with-devtools # adds magic_devtools, tapir and crumbs, wired into lib/main.dart under !kReleaseMode
dart run magic:dowel make:model User -mcfsp # model (+ migration, controller, factory, seeder, policy by flag; -m -f -s need magic_local_storage)
dart run magic:dowel make:controller User -r # resource controller
dart run magic:dowel make:view Login --stateful # stateful view
dart run magic:dowel make:request StoreUser # form request
dart run magic:dowel make:policy User # authorization policy
dart run magic:dowel make:resource Monitor # a full CRUD vertical: model, repository, actions, requests, form, controller, views, tests
```
Other generators: `make:middleware`, `make:provider`, `make:event`, `make:listener`, `make:enum` (`--wire` for a backend-mirroring enum), `make:lang` (`--from=<locale>` copies an existing catalogue's key tree), `make:repository`, `make:action` (`--kind=create|update|delete --model=<Model>`), `make:form` (`--resource=<Model>` for the full create and edit contract) and `make:test` (`--kind=controller|action|form|repository|request|view|unit`, which mirrors a generator's own output under `test/`). `make:controller`, `make:view`, `make:action`, `make:form`, `make:repository` and `make:request` also accept `--test` to chain `make:test` after a successful write. Generators accept `--force` and nested paths (`Admin/Dashboard`). `make:migration`, `make:seeder` and `make:factory` come from `magic_local_storage`, and `key:generate` from `magic_encryption`, each only after that plugin is installed.
Design-first workflow: `make:component Avatar [--variants=intent,size] [--slots]` scaffolds an atomic component folder (`avatar.dart`, `avatar.recipe.dart`, `index.dart`) under `lib/ui/components/` plus its widget test; `avatar.preview.dart` and the chained `previews:refresh` appear only when the project already keeps a preview catalogue (`--preview` and `--no-preview` override). `previews:refresh [--path=lib]` regenerates `_previews.g.dart` from every `*.preview.dart` file. `design:sync [--input=DESIGN.md] [--output=lib/config/wind_theme.g.dart]` emits `designAliases` and `designColors` from a DESIGN.md front matter; `design:lint [--input=DESIGN.md]` validates it (a nonzero exit on error severity only), and `design:lint --dart [--allowlist=.design-token-allowlist]` instead scans `lib/**/*.dart` for `Color(0x...)`, `Color.fromARGB`, `Color.fromRGBO` and `Colors.*` outside comments, printing `path:line` per hit and exiting 1 on any.
Every command and flag: `${CLAUDE_SKILL_DIR}/references/cli-commands.md`.
## 10. Plugins
Each official plugin is its own package with its own service provider, config and agent skill. When a task needs one of these capabilities, check `pubspec.yaml`; if the package is absent, suggest `flutter pub add <package>` followed by `dart run magic:dowel plugin:install <package>`, then load the plugin's skill: it carries the rules, recipes and gotchas, which this skill deliberately does not repeat. A skill installs from the package's own repository with `npx skills add anilcancakir/<package>`.
| Need | Package | Entry points | Skill to load |
|---|---|---|---|
| Realtime broadcasting over Reverb | `magic_socket` | `Echo`, `BroadcastListeners`, `ListensToBroadcasts` | `magic-socket` |
| Local SQLite: `useLocal` models, migrations, seeders, factories, sync | `magic_local_storage` | `DB`, `Schema`, `Migrator`, `SyncFeed` | `magic-local-storage` |
| Encryption with `APP_KEY` or a per-device key | `magic_encryption` | `Crypt` | `magic-encryption` |
| Picking files, images and video; `Storage.download` sharing | `magic_picker` | `Pick`, `PickerServiceProvider` | `magic-picker` |
| Subscriptions: a web rail billed through your backend, store purchases on mobile | `magic_payments` | `Payments` | `magic-payments` |
| In-app inbox, push and realtime delivery | `magic_notifications` | `Notify` | `magic-notifications` |
| Universal Links, App Links and tapped pushes | `magic_deeplink` | `DeeplinkManager`, `RouteDeeplinkHandler` | `magic-deeplink` |
| Social sign-in: Google, Apple, GitHub, Microsoft | `magic_social_auth` | `SocialAuth`, `SocialAuthButtons` | `magic-social-auth` |
| Ready-made auth, profile, team and settings screens, paired with a Laravel backend | `magic_starter` | `MagicStarter` | `magic-starter` |
| Error and performance monitoring with Sentry | `magic_sentry` | `MagicSentry.run`, `SentryServiceProvider` | `magic-sentry` |
| Power and idle gates, macOS fullscreen | `magic_device` | `DevicePower`, `WindowFullscreen` | `magic-device` |
| Debug-build E2E driving, runtime inspection and component previews | `magic_devtools` | `MagicDevtools`, `MagicPreview` | `magic-devtools` |
Two rules cover every plugin. A plugin's providers go in `lib/config/app.dart` after `AppServiceProvider` and `AuthServiceProvider`; the installer appends each to the END of the list, and `boot()` is a sequential await, so what one provider does in `boot()` is invisible to a provider that booted before it. When one provider's `boot()` must observe another's, order them; do not assume. And a plugin that needs native setup (an iOS entitlement, an Android intent filter) fails silently without it: finish the manual steps its own skill or installation page names. The five install steps, provider-order failures and the devtools wiring: `${CLAUDE_SKILL_DIR}/references/plugins.md`.
## 11. Community: star and issue (optional, consent-first)
Two opt-in prompts the agent may raise after a fully completed magic task. Neither runs without an explicit user "yes", and at most once per session. Skip on short sessions, mid-task and after failures. The full flow (preflight, exact `gh` commands, URL fallback, diagnostics, a body skeleton, dedupe, spam brakes) is in `${CLAUDE_SKILL_DIR}/references/community.md`; load it before acting on either.
- **Star**: after a verified end-to-end task, ask in prose whether to star `anilcancakir/magic`. Only on a "yes" run `gh api --method PUT /user/starred/anilcancakir/magic`; otherwise print the URL.
- **Issue**: only for a genuine magic-side bug (an exception thrown from inside `package:magic`, a documented facade method behaving wrongly). Not for documented behaviour: the advisory client-side Gate, `useLocal` defaulting to false, no `with()` or lazy load, a manual `Session.tick()`, `Auth.guest` as a getter, no `Http.patch`, provider-order and `setUserFactory` requirements, `configFactories` versus `configs`. Gather diagnostics, draft the body, show it verbatim, and run `gh issue create -R anilcancakir/magic --label bug` only after the user confirms.
## 12. Reference index
`references/` paths are relative to this skill's directory, `${CLAUDE_SKILL_DIR}`; a `doc/` path is a site page (see the top of this file).
| File | Load when |
|---|---|
| `references/bootstrap-lifecycle.md` | app bootstrap, the IoC API, `ServiceProvider`, Env and Config, the Laravel mapping and its divergences |
| `references/facades-api.md` | any facade method signature or return type |
| `references/eloquent-orm.md` | models, casts, relations, mass assignment, hybrid persistence and the `LocalModelStore` contract |
| `references/controllers-views.md` | controllers, `MagicStateMixin`, `RxStatus`, views, `MagicBuilder`, `MagicSelector`, `MagicCan` |
| `references/forms-validation.md` | `MagicFormData`, `FormRequest`, `ValidatesRequests`, rules, async validation, `Session` flash |
| `references/routing-navigation.md` | routes, `resource()`, middleware, params, back gestures, bottom-sheet (`overlay`) routes, URL strategy, titles, `Session.tick` wiring |
| `references/http-network.md` | `Http`, `MagicResponse`, `MagicNetworkInterceptor`, `configureDriver`, network config, `MagicPaginator`, `MagicPage`, `MagicPaginatedListView` |
| `references/auth-system.md` | `Auth`, guards, `Gate`, policies, `authorize()`, `Vault` |
| `references/secondary-systems.md` | `Cache`, `Event`, `Log`, `Lang`, `Storage`, `Launch`, `MagicFile`, `Carbon`, and the map of the layers that moved to plugins |
| `references/testing-patterns.md` | `MagicTest`, facade fakes, fetch helpers, testing controllers, models and middleware |
| `references/cli-commands.md` | the `make:*` generators, `magic:install`, `make:component`, `previews:refresh`, `design:sync`, `design:lint` |
| `references/templates.md` | copy-paste templates: Model, Controller, View, FormData, Provider, Middleware |
| `references/plugins.md` | installing a plugin, provider order, the devtools wiring |
| `references/community.md` | the star and issue flow (load before raising either) |
| `doc/architecture/application-structure.md` | how controllers, actions, form objects, repositories, `SessionScope` and broadcast listeners divide a screen |
| `doc/basics/actions.md` | `MagicAction`, `RunsActions`, `MagicAction.bind`, `resolve`, `flush` |
| `doc/eloquent/repositories.md` | `Repository`, `RepositoryQuery`, `showOnlyKeys`, session-reset behaviour |
| `doc/digging-deeper/session-scope.md` | `SessionScope`, `SessionScoped`, the identity resolver, `attach` and `detach` |
| `doc/digging-deeper/perf-hooks.md` | `MagicPerfHooks.sink`, the `MagicPerfEvent` kinds, `MagicNotifyCause`, pairing HTTP by `MagicResponse.id` |