-
Notifications
You must be signed in to change notification settings - Fork 26.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unhandled Exception: Binding has not yet been initialized - when running Isolate sample example #145873
Comments
Hi @shashkiranr flutter/packages/flutter/lib/src/services/asset_bundle.dart Lines 315 to 320 in 859a5ab
It's marked as a duplicate of #119207. So I'm going to close the issue in favor of this one. Please follow up on it for updates. |
I am not using any Platform Channels. Its a basic example of running Isolates from the main function. So you are telling me I can't run the sample isolate example? |
The flutter/packages/flutter/lib/src/services/binary_messenger.dart Lines 56 to 60 in 66d1872
You can compare this with a different process like in the sample code below which does I/O task (write to file), the isolate works as expected in this case but reading assets task does not. Sample codeimport 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/foundation.dart';
import 'package:path_provider/path_provider.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// final jsonData = await Isolate.run(() => _readAndParseJson(filename));
// print('Number of JSON keys: ${jsonData.length}');
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int usingTime = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Running time: $usingTime ms"),
TextButton(onPressed: _runWithIsolate, child: const Text("Run with isolate")),
TextButton(
onPressed: _runWithIsolateInPlatformChannel,
child: const Text("Run with isolate in platform channel")),
],
),
),
);
}
void _runWithIsolate() async {
var savePath = await getTemporaryDirectory();
var duration = await compute((path) => _runTask(path), "${savePath.path}/test.log");
print('With isolate duration: $duration');
if (mounted) {
setState(() {
usingTime = duration;
});
}
}
static int _runTask(String savePath) {
var file = File(savePath);
if (file.existsSync()) {
file.deleteSync();
}
file = File(savePath);
var startTime = DateTime.now().millisecondsSinceEpoch;
for (var i = 0; i < 100; i++) {
file.writeAsStringSync("a\n", mode: FileMode.append);
}
return DateTime.now().millisecondsSinceEpoch - startTime;
}
void _runWithIsolateInPlatformChannel() async {
// final jsonData = await Isolate.run(() => _readAndParseJson(filename));
// print('Number of JSON keys: ${jsonData.length}');
final jsonData = await compute(_readAndParseJson, 'json_01.json');
print('Number of JSON keys: ${jsonData}');
}
/// Reads the contents of the file with [filename],
/// decodes the JSON, and returns the result.
static Future<int> _readAndParseJson(String filename) async {
final fileData = await rootBundle.loadString('assets/$filename');
final jsonData = jsonDecode(fileData) as Map<String, String>;
return jsonData.length;
}
}
However, when I check the sample code on https://docs.flutter.dev/perf/isolates which also loads assets in isolate, it seems to have worked before. I will re-open this for further insights. The issue is reproduced in the latest Flutter master channel. flutter doctor -v (stable and master)[✓] Flutter (Channel stable, 3.19.4, on macOS 14.1 23B74 darwin-x64, locale en-VN)
• Flutter version 3.19.4 on channel stable at /Users/huynq/Documents/GitHub/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 68bfaea224 (29 hours ago), 2024-03-20 15:36:31 -0700
• Engine revision a5c24f538d
• Dart version 3.3.2
• DevTools version 2.31.1
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
• Android SDK at /Users/huynq/Library/Android/sdk
• Platform android-34, build-tools 34.0.0
• ANDROID_HOME = /Users/huynq/Library/Android/sdk
• Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 15C500b
• CocoaPods version 1.15.2
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2023.2)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• android-studio-dir = /Applications/Android Studio.app/
• Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)
[✓] VS Code (version 1.87.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.84.0
[✓] Connected device (3 available)
• RMX2001 (mobile) • EUYTFEUSQSRGDA6D • android-arm64 • Android 11 (API 30)
• macOS (desktop) • macos • darwin-x64 • macOS 14.1 23B74 darwin-x64
• Chrome (web) • chrome • web-javascript • Google Chrome 123.0.6312.58
[✓] Network resources
• All expected network resources are available.
• No issues found! [!] Flutter (Channel master, 3.21.0-16.0.pre.14, on macOS 14.1 23B74 darwin-x64, locale en-VN)
• Flutter version 3.21.0-16.0.pre.14 on channel master at /Users/huynq/Documents/GitHub/flutter_master
! Warning: `flutter` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path.
! Warning: `dart` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path.
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision f5b65bad0f (2 hours ago), 2024-03-27 18:20:55 -0700
• Engine revision c602abdbae
• Dart version 3.4.0 (build 3.4.0-279.0.dev)
• DevTools version 2.34.0-dev.12
• If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades.
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
• Android SDK at /Users/huynq/Library/Android/sdk
• Platform android-34, build-tools 34.0.0
• ANDROID_HOME = /Users/huynq/Library/Android/sdk
• Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 15C500b
• CocoaPods version 1.15.2
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2023.2)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• android-studio-dir = /Applications/Android Studio.app/
• Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)
[✓] VS Code (version 1.87.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.84.0
[✓] Connected device (3 available)
• RMX2001 (mobile) • EUYTFEUSQSRGDA6D • android-arm64 • Android 11 (API 30)
• macOS (desktop) • macos • darwin-x64 • macOS 14.1 23B74 darwin-x64
• Chrome (web) • chrome • web-javascript • Google Chrome 123.0.6312.86
[✓] Network resources
• All expected network resources are available.
! Doctor found issues in 1 category. |
The sample code loads the string outside of the isolate and passes it in. There should be no copies with strings, so that should work ok. Closing as this is not a bug or regression. |
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of |
Steps to reproduce
Run the Isolate example
json_01.json
tried to replace
await Isolate.run(() => _readAndParseJson(filename))
withawait compute(_readAndParseJson, filename)
and getting the same errorExpected results
Number of JSON keys: 1
Actual results
Code sample
Code sample
[Paste your code here]
Screenshots or Video
Screenshots / Video demonstration
[Upload media here]
Logs
Logs
[Paste your logs here]
Flutter Doctor output
Doctor output
The text was updated successfully, but these errors were encountered: