build method
Describes the part of the user interface represented by this widget.
The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes). This method can potentially be called in every frame and should not have any side effects beyond building a widget.
The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.
Typically implementations return a newly created constellation of widgets that are configured with information from this widget's constructor and from the given BuildContext.
The given BuildContext contains information about the location in the tree at which this widget is being built. For example, the context provides the set of inherited widgets for this location in the tree. A given widget might be built with multiple different BuildContext arguments over time if the widget is moved around the tree or if the widget is inserted into the tree in multiple places at once.
The implementation of this method must only depend on:
- the fields of the widget, which themselves must not change over time, and
- any ambient state obtained from the
contextusing BuildContext.dependOnInheritedWidgetOfExactType.
If a widget's build method is to depend on anything else, use a StatefulWidget instead.
See also:
- StatelessWidget, which contains the discussion on performance considerations.
Implementation
@override
Widget build(BuildContext context) => DevToolsExtension(
child: Builder(
builder: (BuildContext context) {
// Loads the extension manifest over the live serviceManager VM
// service. When the service / main isolate aren't ready yet,
// throw BindingNotInitializedError → the host shows
// "Binding not detected" rather than an uncaught dart:io
// crash; the probeRetrigger below re-runs it once they are.
Future<List<ExtensionManifestEntry>> probe() async {
final vm = serviceManager.service;
final id = serviceManager.isolateManager.mainIsolate.value?.id;
if (vm == null || id == null) {
throw BindingNotInitializedError();
}
return probeManifest(vm, id);
}
// Builds the in-panel session over the same connection. A
// null service / isolate here is a clear StateError, not an
// "Unsupported operation" crash.
Future<LeonardSession> session() async {
final vm = serviceManager.service;
final id = serviceManager.isolateManager.mainIsolate.value?.id;
if (vm == null || id == null) {
throw StateError(
'VM service / main isolate not available — cannot start '
'a Leonard session yet.',
);
}
return LeonardSession.fromVmService(vm, id);
}
// Config files land at <workspaceRoot>/.dart_tool/<key>.json.
// When DTD is not connected (standalone web / simulated env)
// reads return null and writes are no-ops, leaving the panel
// functional on the in-memory default.
final store = DtdProviderConfigStore(
read: (key) async {
final dtd = dtdManager.connection.value;
if (dtd == null) return null;
final roots = await dtdManager.workspaceRoots();
if (roots == null || roots.ideWorkspaceRoots.isEmpty) {
return null;
}
final uri = Uri.file(
'${roots.ideWorkspaceRoots.first.toFilePath()}'
'/.dart_tool/$key.json',
);
try {
final file = await dtd.readFileAsString(uri, encoding: utf8);
return file.content;
} on RpcException catch (e) {
if (e.code == RpcErrorCodes.kFileDoesNotExist) return null;
rethrow;
}
},
write: (key, value) async {
final dtd = dtdManager.connection.value;
if (dtd == null) return;
final roots = await dtdManager.workspaceRoots();
if (roots == null || roots.ideWorkspaceRoots.isEmpty) return;
final uri = Uri.file(
'${roots.ideWorkspaceRoots.first.toFilePath()}'
'/.dart_tool/$key.json',
);
await dtd.writeFileAsString(uri, value, encoding: utf8);
},
);
// Prompt config: DTD primary (per-workspace file), localStorage
// fallback (per-origin). PromptPanelConfig carries no secrets,
// so plain JSON is safe.
final promptConfigStore = DtdPromptPanelConfigStore(
read: (key) async {
final dtd = dtdManager.connection.value;
if (dtd == null) return null;
final roots = await dtdManager.workspaceRoots();
if (roots == null || roots.ideWorkspaceRoots.isEmpty) {
return null;
}
final uri = Uri.file(
'${roots.ideWorkspaceRoots.first.toFilePath()}'
'/.dart_tool/$key.json',
);
try {
final file = await dtd.readFileAsString(uri, encoding: utf8);
return file.content;
} on RpcException catch (e) {
if (e.code == RpcErrorCodes.kFileDoesNotExist) return null;
rethrow;
}
},
write: (key, value) async {
final dtd = dtdManager.connection.value;
if (dtd == null) return;
final roots = await dtdManager.workspaceRoots();
if (roots == null || roots.ideWorkspaceRoots.isEmpty) return;
final uri = Uri.file(
'${roots.ideWorkspaceRoots.first.toFilePath()}'
'/.dart_tool/$key.json',
);
await dtd.writeFileAsString(uri, value, encoding: utf8);
},
// ignore: deprecated_member_use
localRead: (key) => window.localStorage[key],
// ignore: deprecated_member_use
localWrite: (key, value) {
// ignore: deprecated_member_use
window.localStorage[key] = value;
},
);
return LeonardShell(
manifestProbe: probe,
sessionFactory: session,
store: store,
promptConfigStore: promptConfigStore,
// Reconnects (hot-restart of the target app) flip
// connectedState; the main isolate may appear slightly
// after. The shell listens to either and re-probes the
// manifest. Read inside the Builder so the accesses happen
// after DevToolsExtension.initState — not in the parent's
// build, where serviceManager throws.
probeRetrigger: Listenable.merge(<Listenable?>[
serviceManager.connectedState,
serviceManager.isolateManager.mainIsolate,
]),
);
},
),
);