Skip to content

Allow debugDefaultTargetPlatformOverride to be resetted with addTearDown #110488

Description

@passsy
void main() {
  testWidgets('reset debugDefaultTargetPlatformOverride with tearDown', (tester) async {
    debugDefaultTargetPlatformOverride = TargetPlatform.macOS;
    addTearDown(() {
      debugDefaultTargetPlatformOverride = null;
    });
  });
}

produces

══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following assertion was thrown running a test:
The value of a foundation debug variable was changed by the test.

When the exception was thrown, this was the stack:
#0      debugAssertAllFoundationVarsUnset.<anonymous closure> (package:flutter/src/foundation/debug.dart:30:7)
#1      debugAssertAllFoundationVarsUnset (package:flutter/src/foundation/debug.dart:32:4)
#2      TestWidgetsFlutterBinding._verifyInvariants (package:flutter_test/src/binding.dart:861:12)
#3      AutomatedTestWidgetsFlutterBinding._verifyInvariants (package:flutter_test/src/binding.dart:1274:11)
#4      TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:848:7)
<asynchronous suspension>

The test description was:
  reset debugDefaultTargetPlatformOverride with tearDown
════════════════════════════════════════════════════════════════════════════════════════════════════

I was expecting addTearDown to be the correct solution because setting the override to null might not be executed at the end of the test when the test fails.

In fact - without addTearDown - all remaining tests will fail with the exception above when this test fails, because it never executes debugDefaultTargetPlatformOverride = null;.

void main() {
  testWidgets('reset debugDefaultTargetPlatformOverride with tearDown', (tester) async {
    debugDefaultTargetPlatformOverride = TargetPlatform.macOS;
    // ... test implementation
	expect(true, isFalse);
	
    // might not be called
    debugDefaultTargetPlatformOverride = null;
  });

  testWidgets('default target platform is android', (tester) async {
    // fails with exception although this test is 100% correct
    expect(true, isTrue);
  });
}

How to fix

_verifyInvariants should be called after executing all addTearDown.


[✓] Flutter (Channel master, 3.1.0-0.0.pre.2434, on macOS 12.5.1 21G83
    darwin-arm, locale en-DE)
    • Flutter version 3.1.0-0.0.pre.2434 on channel master at
      /opt/homebrew/Caskroom/flutter/3.0.4/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision c8569b6e98 (6 days ago), 2022-08-23 13:50:09 -0700
    • Engine revision c9d0a01281
    • Dart version 2.19.0 (build 2.19.0-127.0.dev)
    • DevTools version 2.16.0

Activity

  1. added
    in triagePresently being triaged by the triage team
    on Aug 30, 2022
  2. huycozy commented on Aug 30, 2022

    @huycozy
    Member

    @passsy: Thanks for filing the issue. This issue is reproducible on the latest stable and master channels with provided sample code.

    console error logs
    00:02 +0: reset debugDefaultTargetPlatformOverride with tearDown                                                                                     
    ══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
    The following assertion was thrown running a test:
    The value of a foundation debug variable was changed by the test.
    
    When the exception was thrown, this was the stack:
    #0      debugAssertAllFoundationVarsUnset.<anonymous closure> (package:flutter/src/foundation/debug.dart:34:7)
    #1      debugAssertAllFoundationVarsUnset (package:flutter/src/foundation/debug.dart:37:4)
    #2      TestWidgetsFlutterBinding._verifyInvariants (package:flutter_test/src/binding.dart:888:12)
    #3      AutomatedTestWidgetsFlutterBinding._verifyInvariants (package:flutter_test/src/binding.dart:1335:11)
    #4      TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:875:7)
    <asynchronous suspension>
    
    The test description was:
      reset debugDefaultTargetPlatformOverride with tearDown
    ════════════════════════════════════════════════════════════════════════════════════════════════════
    00:02 +0 -1: reset debugDefaultTargetPlatformOverride with tearDown [E]                                                                              
      Test failed. See exception logs above.
      The test description was: reset debugDefaultTargetPlatformOverride with tearDown
    flutter doctor -v
    [✓] Flutter (Channel stable, 3.0.5, on macOS 12.2.1 21D62 darwin-x64, locale en-VN)
        • Flutter version 3.0.5 at /Users/huynq/Documents/GitHub/flutter
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision f1875d570e (9 hours ago), 2022-07-13 11:24:16 -0700
        • Engine revision e85ea0e79c
        • Dart version 2.17.6
        • DevTools version 2.12.2
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
        • Android SDK at /Users/huynq/Library/Android/sdk
        • Platform android-33, build-tools 31.0.0
        • ANDROID_HOME = /Users/huynq/Library/Android/sdk
        • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 13.3)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • CocoaPods version 1.11.3
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2021.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
        • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    
    [✓] IntelliJ IDEA Community Edition (version 2020.3.3)
        • 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
    
    [✓] IntelliJ IDEA Community Edition (version 2022.1.1)
        • IntelliJ at /Users/huynq/Library/Application Support/JetBrains/Toolbox/apps/IDEA-C/ch-0/221.5591.52/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.69.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.44.0
    
    [✓] Connected device (3 available)
        • SM T225 (mobile) • R9JT3004VRJ • android-arm64  • Android 11 (API 30)
        • macOS (desktop)  • macos       • darwin-x64     • macOS 12.2.1 21D62 darwin-x64
        • Chrome (web)     • chrome      • web-javascript • Google Chrome 103.0.5060.114
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
    
    • No issues found!
    
    [✓] Flutter (Channel master, 3.1.0-0.0.pre.2572, on macOS 12.5 21G72 darwin-x64, locale en-EE)
        • Flutter version 3.1.0-0.0.pre.2572 on channel master at /Users/huynq/Documents/GitHub/flutter_master
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision cfee9c8467 (25 minutes ago), 2022-08-29 21:50:23 -0400
        • Engine revision 0bf86adeff
        • Dart version 2.19.0 (build 2.19.0-149.0.dev)
        • DevTools version 2.16.0
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
        • Android SDK at /Users/huynq/Library/Android/sdk
        • Platform android-33, build-tools 31.0.0
        • ANDROID_HOME = /Users/huynq/Library/Android/sdk
        • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 13.3)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 13E113
        • CocoaPods version 1.11.3
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2021.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
        • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    
    [✓] IntelliJ IDEA Community Edition (version 2020.3.3)
        • 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
    
    [✓] IntelliJ IDEA Community Edition (version 2022.1.1)
        • IntelliJ at /Users/huynq/Library/Application Support/JetBrains/Toolbox/apps/IDEA-C/ch-0/221.5591.52/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.70.2)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.46.0
    
    [✓] Connected device (3 available)
        • SM T225 (mobile) • R9JT3004VRJ • android-arm64  • Android 12 (API 31)
        • macOS (desktop)  • macos       • darwin-x64     • macOS 12.5 21G72 darwin-x64
        • Chrome (web)     • chrome      • web-javascript • Google Chrome 104.0.5112.101
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
    
    • No issues found!
    
  3. added
    a: tests"flutter test", flutter_test, or one of our tests
    frameworkflutter/packages/flutter repository. See also f: labels.
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    and removed
    in triagePresently being triaged by the triage team
    on Aug 30, 2022
  4. added
    P3Issues that are less important to the Flutter project
    on Aug 30, 2022
  5. DenisBogatirov commented on Jun 12, 2023

    @DenisBogatirov
  6. passsy commented on Jan 25, 2024

    @passsy
    ContributorAuthor

    My current workaround is a custom addFlutterTearDown which only works with my custom testWidgets2 method

    /// A special version of [addTearDown] that is executed before [testWidgets] completes.
    ///
    /// Only to be used in conjunction with [robotTest].
    ///
    /// It is required for configurations that need to be reset before the flutter test finishes, like:
    ///
    /// - [debugDefaultTargetPlatformOverride]
    /// - [debugImageOverheadAllowance]
    /// - [debugInvertOversizedImages]
    /// - [debugOnPaintImage]
    /// - [debugNetworkImageHttpClientProvider]
    void addFlutterTearDown(dynamic Function() callback) {
      if (Invoker.current == null) {
        throw StateError('addFlutterTearDown() may only be called within a test.');
      }
    
      final list = Zone.current[#flutter_test.teardowns] as List?;
      if (list == null) {
        throw StateError('addFlutterTearDown() may only be called within using testWidgets2');
      }
      list.add(callback);
    }
    import 'package:test_api/src/backend/invoker.dart';
    
    @isTest
    void testWidgets2(
      String description,
      WidgetTesterCallback callback, {
      bool? skip,
      Timeout? timeout,
      bool semanticsEnabled = true,
      dynamic tags,
    }) {
      final List<dynamic Function()> flutterTearDowns = [];
    
      try {
        testWidgets(
          description,
          (tester) async {
            return runZoned(
              () async {
                try {
                  await callback(tester);
                } finally {
                  await Invoker.current!.runTearDowns(flutterTearDowns);
                  flutterTearDowns.clear();
                }
              },
              zoneValues: {
                #flutter_test.teardowns: flutterTearDowns,
              },
            );
          },
          skip: skip,
          timeout: timeout,
          semanticsEnabled: semanticsEnabled,
          tags: tags,
        );
      } finally {
        flutterTearDowns.clear();
      }
    }
  7. hectorAguero commented on Apr 10, 2025

    @hectorAguero

    So I guess P3 means 3 years later at least?

    Is so counterintuitive for overriding debugDefaultTargetPlatformOverride

  8. added a commit that references this issue on Sep 25, 2026
    4f89c8f
  9. added a commit that references this issue on Sep 26, 2026
    5f7822f
  10. added 4 commits that reference this issue on Sep 28, 2026
    6148648
    f65c3af
    31bd0fb
    e1cfe08
  11. github-actions commented on Oct 9, 2026

    @github-actions

    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 flutter doctor -v and a minimal reproduction of the issue.

  12. locked as resolved and limited conversation to collaborators on Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projecta: tests"flutter test", flutter_test, or one of our testsfound in release: 3.0Found to occur in 3.0found in release: 3.1Found to occur in 3.1frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onteam-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions