spawanUri in flutter error with[ERROR:flutter/runtime/dart_isolate.cc(935)] CreateDartIsolateGroup failed: Could not prepare the child isolate to run

23 views Asked by At

calling Isolate.spawnUri in a flutter windows desktop app with a dart or aot file uri result in could not prepare the child isolate exception.

Sample code main.dart:

// ignore_for_file: avoid_print

import 'package:flutter/material.dart';
import 'dart:isolate';

/// Flutter code sample for [IconButton].
void runScript() async {
  try {
    final uri = Uri.parse(
        'file:///C:/flutter_samples/spawnuri_sample/dart_files/script.dart');
    final isolate = await Isolate.spawnUri(uri, [], null);
    final errors = isolate.errors;
    await for (final e in errors) {
      print('error from isolate : ${e.toString()}');
    }
  } catch (e) {
    print('runScript exception: ${e.toString()}');
  }
}

void main() => runApp(const IconButtonExampleApp());

class IconButtonExampleApp extends StatelessWidget {
  const IconButtonExampleApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('IconButton Sample')),
        body: const IconButtonExample(),
      ),
    );
  }
}

class IconButtonExample extends StatelessWidget {
  const IconButtonExample({super.key});

  @override
  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.white,
      child: Center(
        child: Ink(
          decoration: const ShapeDecoration(
            color: Colors.lightBlue,
            shape: CircleBorder(),
          ),
          child: IconButton(
            icon: const Icon(Icons.android),
            color: Colors.white,
            onPressed: () {
              runScript();
            },
          ),
        ),
      ),
    );
  }
}

script.dart content:

// ignore_for_file: avoid_print

import 'dart:io';

void main() {
  print('hello from isolate');
  const duration = Duration(seconds: 5);
  print('Start sleeping');
  sleep(duration);
  print('5 seconds has passed');
  print('bye bye from isolate');
}

Try the same thing with dart cli program instead of flutter. Works with dart file uri in debug, and works with aot file uri in release build.

0

There are 0 answers