Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Enter a scope before calling Dart APIs in ThrowIfUIOperationsProhibited #37226

Merged
merged 1 commit into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/ui/ui_dart_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ void UIDartState::DidSetIsolate() {

void UIDartState::ThrowIfUIOperationsProhibited() {
if (!UIDartState::Current()->IsRootIsolate()) {
Dart_EnterScope();
Dart_ThrowException(
tonic::ToDart("UI actions are only available on root isolate."));
}
Expand Down
11 changes: 11 additions & 0 deletions testing/dart/isolate_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

import 'dart:isolate';
import 'dart:ui';

import 'package:litetest/litetest.dart';

Expand All @@ -20,4 +21,14 @@ void main() {
}
expect(threw, true);
});

test('UI isolate API throws in a background isolate', () async {
void callUiApi(void message) {
PlatformDispatcher.instance.onReportTimings = (_) {};
}
final ReceivePort errorPort = ReceivePort();
await Isolate.spawn<void>(callUiApi, null, onError: errorPort.sendPort);
final List<dynamic> isolateError = await errorPort.first as List<dynamic>;
expect(isolateError[0], 'UI actions are only available on root isolate.');
});
}