Skip to content

[Widget Tests] variant and debugDefaultTargetPlatformOverride do not work as expected leading to failed tests #83788

Closed
flutter/packages
#13278

Description

@abhaysood

I am trying to write a test case for a widget which depends on TargetPlatform. To do so I'm using the "variant" property in widget tests to force the test to run for either iOS or Android.

However when running the tests, one fails while the other passes. Running the tests one at a time passes for both. Which most likely means the variant arguments are not being reset after the test.

I tried running the same using debugDefaultTargetPlatformOverride as described in the answer here, however that also fails for me with the same error.

Sample App

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

void main() {
  runApp(MaterialApp(home: MyApp()));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    if (Theme.of(context).platform == TargetPlatform.iOS) {
      return Container();
    } else {
      return SizedBox();
    }
  }
}

WidgetTest

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets(
    "when platform is iOS",
    (tester) async {
      await tester.pumpWidget(MyApp());
      expect(find.byType(Container), findsOneWidget);
    },
    variant: TargetPlatformVariant.only(TargetPlatform.iOS),
  );

  testWidgets(
    "when platform is Android",
    (tester) async {
      await tester.pumpWidget(MyApp());
      expect(find.byType(SizedBox), findsOneWidget);
    },
    variant: TargetPlatformVariant.only(TargetPlatform.android),
  );
}

Result

══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following TestFailure object was thrown running a test:
  Expected: exactly one matching node in the widget tree
  Actual: _WidgetTypeFinder:<zero widgets with type "SizedBox" (ignoring offstage widgets)>
   Which: means none were found but one was expected

When the exception was thrown, this was the stack:
#4      main.<anonymous closure> (.../test/widget_test.dart:20:7)
<asynchronous suspension>
<asynchronous suspension>
(elided one frame from package:stack_trace)
...

This was caught by the test expectation on the following line:
  .../test/widget_test.dart line 20
The test description was:
  when platform is Android uses MaterialApp (TargetPlatform.android)
════════════════════════════════════════════════════════════════════════════════════════════════════

Test failed. See exception logs above.
The test description was: when platform is Android uses MaterialApp (TargetPlatform.android)

Activity

  1. darshankawar commented on Jun 2, 2021

    @darshankawar
    Member

    Thanks for reporting and code sample.

    console error log
    
    
    Testing started at 6:36 PM ...
    /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter --no-color test --machine --start-paused test/widget_test.dart
    
    ══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
    The following TestFailure object was thrown running a test:
      Expected: exactly one matching node in the widget tree
      Actual: _WidgetTypeFinder:<zero widgets with type "SizedBox" (ignoring offstage widgets)>
       Which: means none were found but one was expected
    
    When the exception was thrown, this was the stack:
    #4      main.<anonymous closure> (file:///Users/dhs/Documents/NCFlutter/macbug/test/widget_test.dart:20:7)
    <asynchronous suspension>
    <asynchronous suspension>
    (elided one frame from package:stack_trace)
    ...
    
    This was caught by the test expectation on the following line:
      file:///Users/dhs/Documents/NCFlutter/macbug/test/widget_test.dart line 20
    The test description was:
      when platform is Android (TargetPlatform.android)
    ════════════════════════════════════════════════════════════════════════════════════════════════════
    
    Test failed. See exception logs above.
    The test description was: when platform is Android (TargetPlatform.android)
    
    
    
    flutter doctor -v
    [✓] Flutter (Channel stable, 2.2.1, on Mac OS X 10.15.4 19E2269 darwin-x64,
        locale en-GB)
        • Flutter version 2.2.1 at /Users/dhs/documents/fluttersdk/flutter
        • Framework revision 02c026b03c (4 days ago), 2021-05-27 12:24:44 -0700
        • Engine revision 0fdb562ac8
        • Dart version 2.13.1
    
    [✓] Xcode - develop for iOS and macOS
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Xcode 12.3, Build version 12C33
        • CocoaPods version 1.10.1
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] VS Code (version 1.55.2)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.21.0
    
    [✓] Connected device (4 available)
        • Darshan's iphone (mobile)  • 21150b119064aecc249dfcfe05e259197461ce23 •
          ios            • iOS 14.4.1
        • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729     •
          ios            • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
        • macOS (desktop)            • macos                                    •
          darwin-x64     • Mac OS X 10.15.4 19E2269 darwin-x64
        • Chrome (web)               • chrome                                   •
          web-javascript • Google Chrome 91.0.4472.77
    
    • No issues found!
    
    
    
    
    
  2. added
    a: tests"flutter test", flutter_test, or one of our tests
    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 Jun 2, 2021
  3. added
    frameworkflutter/packages/flutter repository. See also f: labels.
    on Jan 25, 2022
  4. added
    P2Important issues not at the top of the work list
    on Apr 20, 2022
  5. shashkiranr commented on May 17, 2024

    @shashkiranr

    is there a workaround for this issue? unable to test for iOS variant especially when there are platform specific widgets

  6. tirth-patel-nc commented on May 12, 2025

    @tirth-patel-nc
    Member

    Triage Report

    I was able to reproduce the issue using the provided code sample on both the stable (3.29.3) and master (3.32.0-1.0.pre.494) channels.

    Test Failure Log
    ══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
    The following TestFailure was thrown running a test:
    Expected: exactly one matching candidate
      Actual: _TypeWidgetFinder:<Found 0 widgets with type "SizedBox": []>
       Which: means none were found but one was expected
    
    When the exception was thrown, this was the stack:
    #4      main.<anonymous closure> (file:///Users/tirthpatel/StudioProjects/hello_world/test/widget_test.dart:20:7)
    <asynchronous suspension>
    #5      testWidgets.<anonymous closure>.<anonymous closure> (package:flutter_test/src/widget_tester.dart:193:15)
    <asynchronous suspension>
    #6      TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:1064:5)
    <asynchronous suspension>
    <asynchronous suspension>
    (elided one frame from package:stack_trace)
    
    This was caught by the test expectation on the following line:
      file:///Users/tirthpatel/StudioProjects/hello_world/test/widget_test.dart line 20
    The test description was:
      when platform is Android (variant: TargetPlatform.android)
    ════════════════════════════════════════════════════════════════════════════════════════════════════
    
    Code Sample (Same as OP)
    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MaterialApp(home: MyApp()));
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        if (Theme.of(context).platform == TargetPlatform.iOS) {
          return Container();
        } else {
          return SizedBox();
        }
      }
    }
    Test File (Same as OP)
    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    import 'package:flutter_test/flutter_test.dart';
    import 'package:hello_world/main.dart';
    
    void main() {
      testWidgets(
        "when platform is iOS",
        (tester) async {
          await tester.pumpWidget(MyApp());
          expect(find.byType(Container), findsOneWidget);
        },
        variant: TargetPlatformVariant.only(TargetPlatform.iOS),
      );
    
      testWidgets(
        "when platform is Android",
        (tester) async {
          await tester.pumpWidget(MyApp());
          expect(find.byType(SizedBox), findsOneWidget);
        },
        variant: TargetPlatformVariant.only(TargetPlatform.android),
      );
    }
    stable (3.29.3) - flutter doctor -v
    [✓] Flutter (Channel stable, 3.29.3, on macOS 15.4.1 24E263 darwin-arm64, locale
        en-IN) [970ms]
        • Flutter version 3.29.3 on channel stable at
          /Users/tirthpatel/Development/flutter_stable/flutter
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision ea121f8859 (4 weeks ago), 2025-04-11 19:10:07 +0000
        • Engine revision cf56914b32
        • Dart version 3.7.2
        • DevTools version 2.42.3
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 36.0.0)
        [855ms]
        • Android SDK at /Users/tirthpatel/Library/Android/sdk
        • Platform android-36, build-tools 36.0.0
        • Java binary at: /Applications/Android
          Studio.app/Contents/jbr/Contents/Home/bin/java
          This is the JDK bundled with the latest Android Studio installation on
          this machine.
          To manually set the JDK path, use: `flutter config
          --jdk-dir="path/to/jdk"`.
        • Java version OpenJDK Runtime Environment (build 21.0.6+-13355223-b631.42)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 16.3) [556ms]
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 16E140
        • CocoaPods version 1.16.2
    
    [✓] Chrome - develop for the web [9ms]
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2024.3) [9ms]
        • 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 21.0.6+-13355223-b631.42)
    
    [✓] VS Code (version 1.99.3) [8ms]
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension can be installed from:
          🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter
    
    [✓] Connected device (3 available) [5.5s]
        • macOS (desktop)                 • macos                 • darwin-arm64   •
          macOS 15.4.1 24E263 darwin-arm64
        • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin         •
          macOS 15.4.1 24E263 darwin-arm64
        • Chrome (web)                    • chrome                • web-javascript •
          Google Chrome 135.0.7049.115
    
    [✓] Network resources [559ms]
        • All expected network resources are available.
    
    • No issues found!
    
    master (3.32.0-1.0.pre.494) - flutter doctor -v
    [✓] Flutter (Channel master, 3.32.0-1.0.pre.494, on macOS 15.4.1 24E263 darwin-arm64, locale en-IN) [1,351ms]
        • Flutter version 3.32.0-1.0.pre.494 on channel master at /Users/tirthpatel/Development/flutter_master/flutter
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 4ac6f4d711 (3 hours ago), 2025-05-11 19:38:27 -0700
        • Engine revision 4ac6f4d711
        • Dart version 3.9.0 (build 3.9.0-100.2.beta)
        • DevTools version 2.46.0
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 36.0.0) [771ms]
        • Android SDK at /Users/tirthpatel/Library/Android/sdk
        • Emulator version 35.4.9.0 (build_id 13025442) (CL:N/A)
        • Platform android-36, build-tools 36.0.0
        • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
          This is the JDK bundled with the latest Android Studio installation on this machine.
          To manually set the JDK path, use: `flutter config --jdk-dir="path/to/jdk"`.
        • Java version OpenJDK Runtime Environment (build 21.0.6+-13355223-b631.42)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 16.3) [432ms]
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 16E140
        • CocoaPods version 1.16.2
    
    [✓] Chrome - develop for the web [7ms]
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2024.3) [7ms]
        • 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 21.0.6+-13355223-b631.42)
    
    [✓] VS Code (version 1.100.0) [6ms]
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.110.0
    
    [✓] Connected device (2 available) [5.5s]
        • macOS (desktop) • macos  • darwin-arm64   • macOS 15.4.1 24E263 darwin-arm64
        • Chrome (web)    • chrome • web-javascript • Google Chrome 136.0.7103.93
    
    [✓] Network resources [473ms]
        • All expected network resources are available.
    
    • No issues found!
    

    ❗ I noticed that if I run one test at a time, it works.

  7. github-actions commented on Oct 30, 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.

  8. locked as resolved and limited conversation to collaborators on Oct 30, 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

    P2Important issues not at the top of the work lista: tests"flutter test", flutter_test, or one of our testsfound in release: 3.29Found to occur in 3.29found in release: 3.32Found to occur in 3.32frameworkflutter/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