DartPad Documentation

repository·main·Indexed 23 days ago

https://github.com/dart-lang/dart-pad

An 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.

Tokens
15.7K
Snippets
36
Records
107
Agent score
84%

What's inside DartPad

  1. Overview of DartPad

    main
    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.
  2. Core components of dartpad_editor

    main

    The public API of dartpad_editor consists 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-dart package) before constructing a CodeMirrorEditor.
    • Tab Management: Includes editor tab lifecycle and persistence controllers.
    • Language Server Support: Handles Dart language-server diagnostics and workspace-edit processing.
    • Workspace Management: Provides workspace resources and consolidated file-change events.

    To coordinate editor state, a frontend must supply a concrete WorkspaceController and EditorTabAdapter, then use a TabsController to manage them.

  3. Integrate CodeMirrorLspClient with Dart Language Server

    main

    To support Language Server Protocol (LSP) features, create one CodeMirrorLspClient per workspace.

    1. Add the result of createExtension(uri) to each file's editor extensions.
    2. Forward outbound JSON-RPC messages from sendToServer to the Dart Language Server.
    3. Pass inbound messages from the Dart Language Server back to the client using receiveFromServer.
  4. How the DartPad Frontend workspace works

    main

    The DartPad Frontend uses a two-part rendering process:

    1. Editor Shell: The first browser frame renders the initial editor UI.
    2. DartPad Worker: A worker creates a transient in-memory Flutter project.

    Once the workspace is available, lib/main.dart and pubspec.yaml are opened in reusable editor tabs. LSP (Language Server Protocol) initialization and pub get operations run in the background and do not block the workspace UI.

    Editor Behavior:

    • Saving: Ctrl/Cmd+S saves 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.
  5. Run DartPad UI locally

    main

    To 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-security flag to Chrome to bypass CORS issues related to resource manifests (like AssetManifest.json).

    Using Flutter CLI

    Run the following command:

    flutter run -d chrome --web-browser-flag "--disable-web-security"

    Using VS Code

    1. Open the repository in VS Code.
    2. Select the Run and Debug tab on the left sidebar.
    3. Select the appropriate configuration and run it.
  6. Use the dartpad_editor package

    main

    The dartpad_editor package 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';
  7. Modify supported packages and update dependencies

    main

    Package dependencies are pinned in tool/dependencies/pub_dependencies_<CHANNEL>.yaml files. To add or remove supported packages, follow these steps for each Flutter channel (main, beta, and stable):

    1. Switch to the channel: flutter channel <CHANNEL>
    2. (Optional) Edit lib/src/project_templates.dart to update the whitelist of packages.
    3. Regenerate project templates: dart tool/grind.dart build-project-templates.
    4. If package resolution fails, manually edit the corresponding tool/dependencies/pub_dependencies_<CHANNEL>.yaml file.
    5. Once project_templates/dart_project and project_templates/flutter_project are created, run pub upgrade in each directory.
    6. Update the pinned dependencies: dart tool/grind.dart update-pub-dependencies.
  8. Run the Dart Services server

    main

    The 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/analyze and /api/v3/compile.

    dart bin/server.dart