Repository navigation
Open
Labels
P2Important issues not at the top of the work listImportant issues not at the top of the work lista: tests"flutter test", flutter_test, or one of our tests"flutter test", flutter_test, or one of our testsfound in release: 2.10Found to occur in 2.10Found to occur in 2.10found in release: 2.8Found to occur in 2.8Found to occur in 2.8found in release: 2.9Found to occur in 2.9Found to occur in 2.9found in release: 3.0Found to occur in 3.0Found to occur in 3.0frameworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onThe issue has been confirmed reproducible and is ready to work onteam-frameworkOwned by Framework teamOwned by Framework teamtriaged-frameworkTriaged by Framework teamTriaged by Framework team
Description
Steps to Reproduce
- Create a basic flutter app which wraps
runAppwithrunZonedand provides a custom zone value:
import 'dart:async';
import 'package:flutter/material.dart';
const zoneKey = Object();
void main() {
runZoned(
() => runApp(const MyApp()),
zoneValues: {zoneKey: 'Hello World'},
);
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final zoneValue = Zone.current[zoneKey];
print('MyApp.build() $zoneValue'); // NULL during integration_test run
return const MaterialApp();
}
}- Write an integration test:
import 'package:flutter_test/flutter_test.dart';
import 'package:example/main.dart' as app;
void main() {
group('App', () {
testWidgets('renders MyApp', (tester) async {
app.main();
await tester.pumpAndSettle();
expect(find.byType(app.MyApp), findsOneWidget);
});
});
}- Run the integration test
flutter test integration_test - Observe that the zone value is null within the
buildmethod ofMyApp
Expected results:
I'd expect the zone value to be available within the build method since runApp was executed within the zone.
Actual results:
The zone value is null within the build method during the integration_test run.
Additional Context
Even when the entire test suite is wrapped in runZoned, the zone values are not available during an integration_test run:
void main() {
runZoned(() {
final zoneValue = Zone.current[app.zoneKey];
print('test.main(), $zoneValue'); // Hello Test
group('App', () {
final zoneValue = Zone.current[app.zoneKey];
print('test.group(), $zoneValue'); // Hello Test
testWidgets('renders MyApp', (tester) async {
final zoneValue = Zone.current[app.zoneKey];
print('test.testWidgets(), $zoneValue'); // null
app.main();
await tester.pumpAndSettle();
expect(find.byType(app.MyApp), findsOneWidget);
});
});
}, zoneValues: {app.zoneKey: 'Hello Test'});
}It looks like testWidgets runs the body inside a new zone and does not fork the parent zone and inherit the zoneValues.
Related Issues
- Flutter framework does not warn when ensureInitialized is called in a different zone than runApp #94123
- runZoned with zoneValues lost after hot reload #93676
Logs
{"CFBundleName":"zones","DTXcode":"1310","DTSDKName":"iphonesimulator15.0","UILaunchStoryboardName":"LaunchScreen","CFBundleIcons~ipad":{"CFBundlePrimaryIcon":{"CFBundleIconFiles":["AppIcon20x20","AppIcon29x29","AppIcon40x40","AppIcon60x60","AppIcon76x76","AppIcon83.5x83.
5"],"CFBundleIconName":"AppIcon"}},"DTSDKBuild":"19A339","CFBundleDevelopmentRegion":"en","CFBundleVersion":"1","BuildMachineOSBuild":"21A559","DTPlatformName":"iphonesimulator","CFBundlePackageType":"APPL","UIMainStoryboardFile":"Main","CFBundleSupportedPlatforms":["iPho
neSimulator"],"CFBundleShortVersionString":"1.0.0","CFBundleInfoDictionaryVersion":"6.0","CFBundleExecutable":"Runner","DTCompiler":"com.apple.compilers.llvm.clang.1_0","UISupportedInterfaceOrientations~ipad":["UIInterfaceOrientationPortrait","UIInterfaceOrientationPortra
itUpsideDown","UIInterfaceOrientationLandscapeLeft","UIInterfaceOrientationLandscapeRight"],"MinimumOSVersion":"9.0","CFBundleIdentifier":"com.example.zones","UIDeviceFamily":[1,2],"DTPlatformVersion":"15.0","CFBundleSignature":"????","CFBundleIcons":{"CFBundlePrimaryIcon
":{"CFBundleIconFiles":["AppIcon20x20","AppIcon29x29","AppIcon40x40","AppIcon60x60"],"CFBundleIconName":"AppIcon"}},"DTXcodeBuild":"13A1030d","LSRequiresIPhoneOS":true,"UISupportedInterfaceOrientations":["UIInterfaceOrientationPortrait","UIInterfaceOrientationLandscapeLef
t","UIInterfaceOrientationLandscapeRight"],"UIViewControllerBasedStatusBarAppearance":false,"NSBonjourServices":["_dartobservatory._tcp"],"DTPlatformBuild":"19A339","NSLocalNetworkUsageDescription":"Allow Flutter tools on your computer to connect and debug your
application. This prompt will not appear on release builds."}
[ +4 ms] executing: xcrun simctl launch FCB4BE3E-33DA-49FC-A861-F647555D56A9 com.example.zones --enable-dart-profiling --enable-checked-mode --verify-entry-points --observatory-port=0
[ +205 ms] com.example.zones: 51465
[ +1 ms] Waiting for observatory port to be available...
[ +416 ms] Observatory URL on device: http://127.0.0.1:55348/QIoVQ24XVz0=/
[ +9 ms] Caching compiled dill
[ +51 ms] Connecting to service protocol: http://127.0.0.1:55348/QIoVQ24XVz0=/
[ +167 ms] Launching a Dart Developer Service (DDS) instance at http://127.0.0.1:0, connecting to VM service at http://127.0.0.1:55348/QIoVQ24XVz0=/.
[ +95 ms] DDS is listening at http://127.0.0.1:55351/1RcuH9jFcOE=/.
[ +59 ms] Successfully connected to service protocol: http://127.0.0.1:55348/QIoVQ24XVz0=/
[ +26 ms] DevFS: Creating new filesystem on the device (null)
[ +17 ms] DevFS: Created new filesystem on the device (file:///Users/felix/Library/Developer/CoreSimulator/Devices/FCB4BE3E-33DA-49FC-A861-F647555D56A9/data/Containers/Data/Application/42BE9414-534D-477C-ADEB-EDC73F58B7FA/tmp/zonesEyB4Fm/zones/)
[ +2 ms] Updating assets
[ +63 ms] Syncing files to device iPhone 13...
[ +2 ms] <- reset
[ ] Compiling dart to kernel with 0 updated files
[ +3 ms] <- recompile package:zones/main.dart 7fd16ad6-36b9-4ac5-99dd-f42726cef603
[ ] <- 7fd16ad6-36b9-4ac5-99dd-f42726cef603
[ +64 ms] Updating files.
[ ] DevFS: Sync finished
[ ] Syncing files to device iPhone 13... (completed in 71ms)
[ ] Synced 0.0MB.
[ +1 ms] <- accept
[ +6 ms] Connected to _flutterView/0x7fb30a810c20.
[ +3 ms] Flutter run key commands.
[ +1 ms] r Hot reload. 🔥🔥🔥
[ ] R Hot restart.
[ ] h List all available interactive commands.
[ ] d Detach (terminate "flutter run" but leave application running).
[ ] c Clear the screen
[ ] q Quit (terminate the application on the device).
[ ] 💪 Running with sound null safety 💪
[ ] An Observatory debugger and profiler on iPhone 13 is available at: http://127.0.0.1:55351/1RcuH9jFcOE=/
[ +490 ms] The Flutter DevTools debugger and profiler on iPhone 13 is available at: http://127.0.0.1:9102?uri=http://127.0.0.1:55351/1RcuH9jFcOE=/
[+4486 ms] Skipping target: gen_localizations
[ ] Skipping target: gen_dart_plugin_registrant
[ ] Skipping target: _composite
[ ] complete
[ ] Performing hot reload...
[ +11 ms] Scanned through 528 files in 6ms
[ ] Syncing files to device iPhone 13...
[ ] Compiling dart to kernel with 0 updated files
[ ] <- recompile package:zones/main.dart 564d0ee5-dbd9-4020-bfeb-4217c387483f
[ ] <- 564d0ee5-dbd9-4020-bfeb-4217c387483f
[ +6 ms] Updating files.
[ +2 ms] DevFS: Sync finished
[ ] Syncing files to device iPhone 13... (completed in 9ms)
[ ] Synced 0.0MB.
[ +11 ms] Reassembling application
[ +39 ms] Hot reload performed in 67ms.
[ +1 ms] Performing hot reload... (completed in 73ms)
[ ] Reloaded 0 libraries in 78ms.
No issues found! (ran in 1.9s)
[✓] Flutter (Channel beta, 2.7.0-3.1.pre, on macOS 12.0.1 21A559 darwin-x64, locale en-US)
• Flutter version 2.7.0-3.1.pre at /Users/felix/fvm/versions/beta
• Upstream repository [email protected]:flutter/flutter.git
• Framework revision fc7015e35a (3 weeks ago), 2021-10-27 15:18:41 -0700
• Engine revision a75e3b80f9
• Dart version 2.15.0 (build 2.15.0-178.1.beta)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
• Android SDK at /Users/felix/Library/Android/sdk
• Platform android-30, build-tools 29.0.2
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 13.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• CocoaPods version 1.11.2
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 4.1)
• 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
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
[✓] IntelliJ IDEA Community Edition (version 2021.2)
• IntelliJ at /Applications/IntelliJ IDEA CE.app
• 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
[✓] VS Code (version 1.60.1)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.27.0
[✓] VS Code (version 1.63.0-insider)
• VS Code at /Applications/Visual Studio Code - Insiders.app/Contents
• Flutter extension version 3.28.0
[✓] Connected device (3 available)
• iPhone 13 (mobile) • FCB4BE3E-33DA-49FC-A861-F647555D56A9 • ios • com.apple.CoreSimulator.SimRuntime.iOS-15-0 (simulator)
• macOS (desktop) • macos • darwin-x64 • macOS 12.0.1 21A559 darwin-x64
• Chrome (web) • chrome • web-javascript • Google Chrome 95.0.4638.69
• No issues found!
Reacted by Marcos Sevilla, Dominik Roszkowski, Scarlett Eliza, Felix Angelov, Allison Ryan, Elias Reis, Tom Arra, Kyle Finlinson, Deniz Bilgin, Joanna May and 19 more
Metadata
Metadata
Assignees
Labels
P2Important issues not at the top of the work listImportant issues not at the top of the work lista: tests"flutter test", flutter_test, or one of our tests"flutter test", flutter_test, or one of our testsfound in release: 2.10Found to occur in 2.10Found to occur in 2.10found in release: 2.8Found to occur in 2.8Found to occur in 2.8found in release: 2.9Found to occur in 2.9Found to occur in 2.9found in release: 3.0Found to occur in 3.0Found to occur in 3.0frameworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onThe issue has been confirmed reproducible and is ready to work onteam-frameworkOwned by Framework teamOwned by Framework teamtriaged-frameworkTriaged by Framework teamTriaged by Framework team
Activity
danagbemava-nc commentedon Jan 21, 2022
Hi @felangel, you appear to be on an older version of beta.
Can you upgrade to the latest version to see if you still experience this issue?
Thank you
felangel commentedon Jan 25, 2022
Hey @danagbemava-nc sorry for the delayed response.
I just verified that I'm seeing the same result on the master channel:
Hixie commentedon Jan 25, 2022
I suspect the same would happen in a unit test (with
flutter_test). The problem is likely thatZone.currentis not the zone you are expecting, because integration_test and flutter_test make heavy use of zones of their own. You might be able to find the data by walking up the chain of Zones until you get to the one you expect.(In general I personaly would recommend against using Zones, they are a never-ending source of pain in my experience. Better to just inject an InheritedWidget and/or pass the data via arguments.)
felangel commentedon Jan 25, 2022
Shouldn't
integration_testandflutter_testrespect the current zone they are in and fork it (preserving the zone values)?This is not feasible because this is an issue in the context of a pure Dart package being used within a Flutter application. The implementation is pure Dart.
danagbemava-nc commentedon Jan 26, 2022
Reproducible using sample and steps in #96939 (comment).
code samples
main.dartintegration_test/app_test.dartlogs
run_log.txt
test_log.txt
flutter doctor -v
Hixie commentedon Jan 26, 2022
Hm, as far as I can tell from the code, they do. Weird. Maybe there's somewhere where we fail to do that?
Might be helpful to walk the zone tree at the place where you're not finding your values, and see what the tree contains and where the values get dropped.
I mean, you can always use arguments, even in pure Dart. :-)
51 remaining items
Hixie commentedon Jun 21, 2022
@bartekpacia I assure you if there was news we would have posted it. :-)
That said, if you are interested in contributing a fix, I'm sure we would be open to reviewing it. The first step is to figure out exactly what the fix should even be.
bartekpacia commentedon Jul 21, 2022
I don't doubt this 😁
I'd love to, but I'm not familiar with the internals of the framework (and don't have time to dive into it) :(
Also, this also occurs in Flutter 2.10 and 3.0, so you could update the labels :)
bartekpacia commentedon Feb 20, 2023
Might be related to dart-lang/test#1023?
flutter-triage-bot commentedon Jul 17, 2023
This issue is marked P1 but has had no recent status updates.
The P1 label indicates high-priority issues that are at the top of the work list. This is the highest priority level a bug can have if it isn't affecting a top-tier customer or breaking the build. Bugs marked P1 are generally actively being worked on unless the assignee is dealing with a P0 bug (or another P1 bug). Issues at this level should be resolved in a matter of months and should have monthly updates on GitHub.
Please consider where this bug really falls in our current priorities, and label it or assign it accordingly. This allows people to have a clearer picture of what work is actually planned. Thanks!
lrhn commentedon Feb 14, 2025
Drive-by comment, since someone linked to this issue ...
That's not necessarily a problem, they should just always create a new zone when used in a new context, and always fork the current zone, not some other zone (like the root zone), to avoid throwing away any existing zone context.
A new zone can still refer to another older zone, either if that's a transitive parent zone, or they can share a state if they're related.