Repository navigation
Closed
flutter/packages
#13278Labels
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: 3.29Found to occur in 3.29Found to occur in 3.29found in release: 3.32Found to occur in 3.32Found to occur in 3.32frameworkflutter/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
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)
Reacted by Hubert Jóźwiak, Shashi Kiran R and Tirth Patel
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: 3.29Found to occur in 3.29Found to occur in 3.29found in release: 3.32Found to occur in 3.32Found to occur in 3.32frameworkflutter/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
darshankawar commentedon Jun 2, 2021
Thanks for reporting and code sample.
console error log
flutter doctor -v
shashkiranr commentedon May 17, 2024
is there a workaround for this issue? unable to test for iOS variant especially when there are platform specific widgets
tirth-patel-nc commentedon May 12, 2025
Triage Report
I was able to reproduce the issue using the provided code sample on both the
stable (3.29.3)andmaster (3.32.0-1.0.pre.494)channels.Test Failure Log
Code Sample (Same as OP)
Test File (Same as OP)
stable (3.29.3) - flutter doctor -v
master (3.32.0-1.0.pre.494) - flutter doctor -v
❗ I noticed that if I run one test at a time, it works.
Make the fallback theme follow defaultTargetPlatform
[material_ui] Fallback theme follows platform cherry pick (#13278)
Make the `Material` fallback theme follow `defaultTargetPlatform` (fl…
Make the `Material` fallback theme follow `defaultTargetPlatform` (fl…
github-actions commentedon Oct 30, 2026
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 -vand a minimal reproduction of the issue.