OpenFlutter / flutter_screenutil

Flutter screen adaptation, font adaptation, get screen information
https://pub.dartlang.org/packages/flutter_screenutil
Apache License 2.0
3.85k stars 486 forks source link

widget tests failing after updating to 5.9.0 #515

Closed mleevi closed 9 months ago

mleevi commented 11 months ago

Widget tests that contain ScreenUtilInit fail when using 5.9.0. Downgrading to 5.8.4 fixes the issue. Putting a tester.pumpAndSettle() after every tester.pumpWidget also solves the problem.

Mounir-Bouaiche commented 11 months ago

What kind of errors?

mleevi commented 11 months ago

What kind of errors?

Finder does not find anything on the first pump (zero widgets found). After adding pumpAndsSettle after the initial pump fixes the errors.

Mounir-Bouaiche commented 11 months ago

Put here a minimal code to reproduce the error.

mleevi commented 11 months ago

Passes with 5.8.4, fails with 5.9.0:

class Test extends StatefulWidget {
  const Test({super.key});

  @override
  State<Test> createState() => _TestState();
}

class _TestState extends State<Test> {
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(430, 932),
      minTextAdapt: true,
      splitScreenMode: true,
      useInheritedMediaQuery: true,
      child: const Scaffold(body: Text('text')),
      builder: (_, child) => MaterialApp(
        localizationsDelegates: const [
          GlobalCupertinoLocalizations.delegate,
          DefaultCupertinoLocalizations.delegate,
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
        ],
        home: child,
      ),
    );
  }
}

void main() {
  testWidgets('description', (tester) async {
    await tester.pumpWidget(const Test());

    expect(find.text('text'), findsOneWidget);
  });
}
Mounir-Bouaiche commented 11 months ago

Well, that's unexpected. We're using Future in last version for the new change: ensureScreenSize as parameter. Thanks for reporting.

mleevi commented 10 months ago

Is there any update on this? @Mounir-Bouaiche @techouse @lizhuoyuan @naivetoby

github-actions[bot] commented 9 months ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 9 months ago

This issue was closed because it has been inactive for 14 days since being marked as stale.

ZG-123 commented 5 months ago

Hello everyone, how did this work out in the end? I also encountered the same problem.

gustamor commented 5 months ago

The same issue:

[✓] Flutter (Channel stable, 3.16.9, on macOS 14.2.1 23C71 darwin-arm64, locale es-ES)
    • Flutter version 3.16.9 on channel stable at /Users/gus/SDK/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 41456452f2 (13 days ago), 2024-01-25 10:06:23 -0800
    • Engine revision f40e976bed
    • Dart version 3.2.6
    • DevTools version 2.28.5

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/gus/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.0.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15A507
    • CocoaPods version 1.14.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.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 17.0.7+0-17.0.7b1000.6-10550314)

[✓] VS Code (version 1.86.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.82.0

[✓] Network resources
    • All expected network resources are available.

With the test:

void main() {
  testWidgets('should sign screen widgets exist', (WidgetTester tester) async {
    await tester.pumpWidget(
      const CupertinoApp(
        home: SignPortraitScreen(),
      ),
    );
    final scaffoldFinder = find.byKey(const Key('scaffold_sign_portrait'));
    expect(scaffoldFinder, findsOneWidget);
    final navTitleFinder = find.byKey(const Key('sign_nav_title'));
    expect(navTitleFinder, findsOneWidget);
  });
}

The test passes when SignPortraitScreen() does not use any screen utilities, with a value like height: 100, and fails when it has any value such as height: 100.h.

Error:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following LateError was thrown building SignPortraitScreen(dirty):
LateInitializationError: Field '_splitScreenMode@289084504' has not been initialized.

The relevant error-causing widget was:
  SignPortraitScreen
  SignPortraitScreen:file:///Users/gus/Codigo/FlutterProjects/project/test/widgets/sign_screen_portrait_test.dart:11:15

When the exception was thrown, this was the stack:
#0      ScreenUtil._splitScreenMode (package:flutter_screenutil/src/screen_util.dart)
#1      ScreenUtil.scaleHeight (package:flutter_screenutil/src/screen_util.dart:204:8)
#2      ScreenUtil.setHeight (package:flutter_screenutil/src/screen_util.dart:225:44)
#3      SizeExtension.h (package:flutter_screenutil/src/size_extension.dart:12:32)
#4      SignPortraitScreen.build (package:story_creator/ui/screens/login/sign_portrait_screen.dart:33:31)
#5      StatelessElement.build (package:flutter/src/widgets/framework.dart:5541:49)
#6      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5471:15)

Always fails with:

void main() {
  testWidgets('should sign screen widgets exist', (WidgetTester tester) async {
    await tester.pumpWidget(
      const CupertinoApp(
        home: ScreenUtilInit(
          child: SignPortraitScreen(),
        ),
      ),
    );
    final scaffoldFinder = find.byKey(const Key('scaffold_sign_portrait'));
    expect(scaffoldFinder, findsOneWidget);
    final navTitleFinder = find.byKey(const Key('sign_nav_title'));
    expect(navTitleFinder, findsOneWidget);
  });
}

Then:

══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following TestFailure was thrown running a test:
Expected: exactly one matching candidate
  Actual: _KeyWidgetFinder:<Found 0 widgets with key [<'scaffold_sign_portrait'>]: []>
   Which: means none were found but one was expected

When the exception was thrown, this was the stack:
#4      main.<anonymous closure> (file:///Users/gus/Codigo/FlutterProjects/project/test/widgets/sign_screen_portrait_test.dart:16:5)
<asynchronous suspension>
#5      testWidgets.<anonymous closure>.<anonymous closure> (package:flutter_test/src/widget_tester.dart:168:15)
<asynchronous suspension>
#6      TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:1013: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/gus/Codigo/FlutterProjects/project/test/widgets/sign_screen_portrait_test.dart line 16
The test description was:
  should sign screen widgets exist
════════════════════════════════════════════════════════════════════════════════════════════════════

Works fine with tester.pumpAndSettle():

testWidgets('should sign screen widgets exist', (WidgetTester tester) async {
    await tester.pumpWidget(
      const CupertinoApp(
        home: ScreenUtilInit(
          child: SignPortraitScreen(),
        ),
      ),
    );

    await tester.pumpAndSettle();

    final scaffoldFinder = find.byKey(const Key('scaffold_sign_portrait'));
    expect(scaffoldFinder, findsOneWidget);
    final navTitleFinder = find.byKey(const Key('sign_nav_title'));
    expect(navTitleFinder, findsOneWidget);
  });
MohiuddinM commented 4 months ago

@gustamor @ZG-123 This issue has been fixed on master for quite some time, but unfortunately, maintainers did not have the time to make a release.

MohiuddinM commented 4 months ago

@Mounir-Bouaiche Is it possible to make a release or is this package now unmaintained?

leventkantaroglu commented 3 months ago

@gustamor @ZG-123 This issue has been fixed on master for quite some time, but unfortunately, maintainers did not have the time to make a release.

What you mean with "on master"? I couldn't see any commit on master branch.