Web Packages
Web packages are reusable Dart packages that bundle Flutter widgets, JavaScript factories, and optional CSS assets for Fluttron apps.
What Is Implemented
Web Package support is fully integrated in the CLI build flow:
fluttron create --type web_packagescaffold- Dependency discovery from
ui/.dart_tool/package_config.json - Manifest + marker validation (
fluttron_web_package.json+fluttron_web_package: true) - Asset collection into
ext/packages/<package>/... - HTML injection for package JS/CSS
- Auto-generation of
ui/lib/generated/web_package_registrations.dart - Diagnostics command:
fluttron packages list -p <app>
Create a Web Package
fluttron create ./my_editor --name my_editor --type web_package
cd my_editor
dart pub get
cd frontend
pnpm install
pnpm run js:build
Notes:
- Template
pubspec.yamlincludesfluttron_web_package: true. - Template default is
publish_to: none(MVP distribution is path/git first). createrewrites local Fluttron dependency paths so local builds work immediately.
Package Structure
my_editor/
├── fluttron_web_package.json
├── pubspec.yaml
├── lib/
│ ├── my_editor.dart
│ └── src/
│ └── example_widget.dart
├── frontend/
│ ├── package.json
│ ├── scripts/build-frontend.mjs
│ └── src/main.js
└── web/
└── ext/
├── main.js
└── main.css
Manifest Contract
fluttron_web_package.json:
{
"version": "1",
"viewFactories": [
{
"type": "my_editor.editor",
"jsFactoryName": "fluttronCreateMyEditorEditorView",
"description": "Editor component"
}
],
"assets": {
"js": ["web/ext/main.js"],
"css": ["web/ext/main.css"]
},
"events": [
{
"name": "fluttron.my_editor.editor.change",
"direction": "js_to_dart",
"payloadType": "{ content: string }"
}
]
}
Field summary:
version: must be"1"viewFactories: required, at least oneassets.js: requiredassets.css: optionalevents: optional
Integrate Into an App
- Add dependency in
ui/pubspec.yaml:
dependencies:
my_editor:
path: ../../my_editor
- Resolve UI dependencies:
cd my_app/ui
flutter pub get
- Build app:
cd ..
fluttron build -p .
During fluttron build, the pipeline runs:
- UI frontend build (
pnpm run js:build) - UI source JS validation
- Web package discovery
- Registration generation
flutter build web- Package asset collection
- HTML injection
- Build output JS validation
- Copy to
host/assets/www - Host
pubspec.yamlasset declaration sync for discovered packages - Host asset JS validation
If step 10 updates host pubspec.yaml, run:
cd my_app/host
flutter pub get
Auto Registration
Generated file:
ui/lib/generated/web_package_registrations.dart
Template apps already import and call:
import 'generated/web_package_registrations.dart';
void main() {
registerFluttronWebPackages();
runFluttronUi(title: 'My App', home: const MyHomePage());
}
JavaScript Factory Contract
Register function name in manifest without window. prefix, for example:
jsFactoryName: "fluttronCreateMyEditorEditorView"
Expose it globally in JS:
window.fluttronCreateMyEditorEditorView = function(viewId, initialContent) {
const container = document.createElement('div');
container.id = `my-editor-${viewId}`;
return container;
};
Conflict Strategy
Type conflicts are strict:
- Same
type+ samejsFactoryName: idempotent - Same
type+ differentjsFactoryName: throwsStateError
This is enforced by FluttronWebViewRegistry at runtime.
Diagnose Dependencies
List discovered web packages:
fluttron packages list -p ./my_app
Output includes package name, version, and exposed view factory types.
MVP Distribution Scope
Current supported distribution paths:
- Path dependencies (local development)
- Git dependencies
Out of current MVP scope:
- Direct pub.dev distribution workflow
Troubleshooting
Package not discovered
Check all of the following in the package root:
fluttron_web_package.jsonexistspubspec.yamlcontainsfluttron_web_package: trueui/.dart_tool/package_config.jsonis present (flutter pub getinui/)
View factory not found
Check:
jsFactoryNamein manifest does not includewindow.- Corresponding function is attached on
windowin JS web/ext/main.jswas rebuilt (pnpm run js:build)
Asset injection missing
Run:
fluttron build -p ./my_app
fluttron packages list -p ./my_app
Then inspect:
host/assets/www/index.htmlhost/assets/www/ext/packages/<package>/...
Host runtime cannot load package assets
Check:
host/pubspec.yamlcontainsassets/www/ext/packages/andassets/www/ext/packages/<package>/- You ran
flutter pub getinsidehost/after build updated hostpubspec.yaml - Target files exist under
host/assets/www/ext/packages/<package>/