DartPad Documentation
repository·main·Indexed 23 days ago
https://github.com/dart-lang/dart-padAn open-source online playground for Dart and Flutter. This repository includes the Dart Services stateless backend for static analysis and compilation, the DartPad Frontend, and supporting packages such as dartpad_editor, dartpad_shared, and codemirror-dart for CodeMirror 6 integration.
What's inside DartPad
- DartPad is a free, open-source online editor designed to help developers learn Dart and Flutter. It provides an environment to compile, analyze, and display the results of Dart code directly in the browser. DartPad can be accessed at dartpad.dev or embedded into other websites using an iframe.
Core components of dartpad_editor
mainThe public API of
dartpad_editorconsists of several key functional areas:- CodeMirror Integration: Provides CodeMirror editor and code-action integration. Note that the CodeMirror JavaScript bundle must be loaded (as described in the
codemirror-dartpackage) before constructing aCodeMirrorEditor. - Tab Management: Includes editor tab lifecycle and persistence controllers.
- Language Server Support: Handles Dart language-server diagnostics and
workspace-editprocessing. - Workspace Management: Provides workspace resources and consolidated file-change events.
To coordinate editor state, a frontend must supply a concrete
WorkspaceControllerandEditorTabAdapter, then use aTabsControllerto manage them.- CodeMirror Integration: Provides CodeMirror editor and code-action integration. Note that the CodeMirror JavaScript bundle must be loaded (as described in the
Integrate CodeMirrorLspClient with Dart Language Server
mainTo support Language Server Protocol (LSP) features, create one
CodeMirrorLspClientper workspace.- Add the result of
createExtension(uri)to each file's editor extensions. - Forward outbound JSON-RPC messages from
sendToServerto the Dart Language Server. - Pass inbound messages from the Dart Language Server back to the client using
receiveFromServer.
- Add the result of
How the DartPad Frontend workspace works
mainThe DartPad Frontend uses a two-part rendering process:
- Editor Shell: The first browser frame renders the initial editor UI.
- DartPad Worker: A worker creates a transient in-memory Flutter project.
Once the workspace is available,
lib/main.dartandpubspec.yamlare opened in reusable editor tabs. LSP (Language Server Protocol) initialization andpub getoperations run in the background and do not block the workspace UI.Editor Behavior:
- Saving:
Ctrl/Cmd+Ssaves all dirty tabs and automatically formats Dart files. - Closing Tabs: Closing a tab with unsaved changes (a 'dirty' tab) requires user confirmation.
- Reloading: Reloading the page resets the transient project.
Enable code generation features
mainTo test code generation features locally, you must provide a Gemini API key via the
GEMINI_API_KEYenvironment variable before starting the server.export GEMINI_API_KEY=<YOUR_GEMINI_API_KEY> dart bin/server.dartRun DartPad UI locally
mainTo run the DartPad web UI locally, you can use the Flutter CLI or VS Code. When running via the CLI, you must pass the
--disable-web-securityflag to Chrome to bypass CORS issues related to resource manifests (likeAssetManifest.json).Using Flutter CLI
Run the following command:
flutter run -d chrome --web-browser-flag "--disable-web-security"Using VS Code
- Open the repository in VS Code.
- Select the Run and Debug tab on the left sidebar.
- Select the appropriate configuration and run it.
Use the dartpad_editor package
mainThe
dartpad_editorpackage provides building blocks for client-side DartPad, including CodeMirror editor integration, language-server diagnostics, and workspace management.Note: This is a private workspace package and is not published to pub.dev.
To use the package, always import the package entrypoint rather than implementation files located under
lib/src:import 'package:dartpad_editor/dartpad_editor.dart';import 'package:dartpad_editor/dartpad_editor.dart';Embed DartPad in your website
mainYou can embed DartPad into other websites using an iframe. For detailed instructions on how to implement this, refer to the Embedding Guide.Validate SDK assets
mainTo verify existing SDK assets (for CI or local verification) without rebuilding them, use the
--validate-onlyflag with the asset building tool.dart run tool/build_sdk_assets.dart --validate-onlyModify supported packages and update dependencies
mainPackage dependencies are pinned in
tool/dependencies/pub_dependencies_<CHANNEL>.yamlfiles. To add or remove supported packages, follow these steps for each Flutter channel (main,beta, andstable):- Switch to the channel:
flutter channel <CHANNEL> - (Optional) Edit
lib/src/project_templates.dartto update the whitelist of packages. - Regenerate project templates:
dart tool/grind.dart build-project-templates. - If package resolution fails, manually edit the corresponding
tool/dependencies/pub_dependencies_<CHANNEL>.yamlfile. - Once
project_templates/dart_projectandproject_templates/flutter_projectare created, runpub upgradein each directory. - Update the pinned dependencies:
dart tool/grind.dart update-pub-dependencies.
- Switch to the channel:
Run the DartPad Frontend project
mainYou can run the DartPad Frontend project using the
jaspr_clipackage to serve the application locally.dart run jaspr_cli:jaspr serve -vRun the Dart Services server
mainThe Dart Services server is a stateless Dart backend that powers DartPad. It provides features like static analysis, JavaScript compilation, code completion, dartdoc information, code formatting, and quick fixes. By default, the server runs on port 8080 and exposes JSON APIs such as
/api/v3/analyzeand/api/v3/compile.dart bin/server.dart