Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LateInitializationError: Field '_instance@1894507694' has not been initialized #1673

Open
4 tasks
hadiabdulla opened this issue Jan 7, 2025 · 0 comments
Open
4 tasks
Labels
new issue An issue that hasn't yet been seen from the maintainer

Comments

@hadiabdulla
Copy link

Describe the bug
A LateInitializationError occurs during a Flutter test when attempting to pick a file using the file_picker package. The error is caused by the uninitialized field _instance in the FilePicker class. The file pick is working perfectly but the test case is giving error.

Platform

  • Android
  • iOS
  • Web
  • Desktop

Platform OS version

How are you picking?
this is the test widget

testWidgets(
        'renders correctly when file pick in file edit is failed due to excess file size',
        (WidgetTester tester) async {
      when(() => mockPlanBloc.state)
          .thenReturn(PlanLoadSuccess(maxShortUrlsAllowed: mockPlanData));
      when(() => mockShortxBloc.state).thenReturn(
        const ShortxFilePickFailed(errorMessage: 'File size exceeded'),
      );
      when(() => mockShortxTagBloc.state)
          .thenReturn(ShortxCurrentTagsSuccess(tags: const [
        "tags1",
        "tags2",
        "tags3"
      ], selectedTags: const [
        "tags100",
        "tags101",
      ]));
      when(() => mockDomainListBloc.state).thenReturn(
        DomainListInitial(),
      );
      await tester.pumpWidget(MultiBlocProvider(
        providers: [
          BlocProvider.value(value: mockShortxBloc),
          BlocProvider.value(value: mockShortxTagBloc),
          BlocProvider.value(value: mockDomainListBloc),
          BlocProvider.value(value: mockPlanBloc),
        ],
        child: MaterialApp(
          home: Scaffold(
              body: OriginalUrlWidget(
            url: 'www.socxo.com',
            type: 2,
            urlController: TextEditingController(),
            fileName: null,
          )),
        ),
      ));
      await tester.pump();
      await tester.pump(const Duration(seconds: 10));
      expect(find.text('Click to Edit file'), findsOneWidget);
      await tester.tap(find.text('Click to Edit file'));
      await tester.pumpAndSettle();
      expect(find.text('File size exceeded'), findsOneWidget);
    });

I'm using this code to pick the file

static Future<ImagePickerResult?> pickCustomFile(
      {required List<String> allowedExtension,
      required double maxFileSizeLimit,
      required String previousPath}) async {
    FilePickerResult? result = await FilePicker.platform.pickFiles(
      type: FileType.custom,
      allowedExtensions: allowedExtension,
    );
    if (result != null) {
      final size = result.files.first.size;
      final fileExtension =
          getFileExtension(result.files.first.path!).toLowerCase();
      bool isExtensionAllowed = allowedExtension.contains(fileExtension);
      final sizeKbs = size / 1000;
      if (isExtensionAllowed) {
        if (sizeKbs <= maxFileSizeLimit) {
          return ImagePickerResult(
              path: result.files.single.path,
              extension: result.files.single.extension,
              filePickerError: null);
        } else {
          return ImagePickerResult(
              path: null,
              extension: null,
              filePickerError: FilePickerError.fileSizeExceeded);
        }
      } else {
        return ImagePickerResult(
            path: null,
            extension: null,
            filePickerError: FilePickerError.unsupportedFileType);
      }
    } else {
      return ImagePickerResult(
          path: previousPath == '' ? null : previousPath,
          extension: previousPath == '' ? null : getFileExtension(previousPath),
          filePickerError: FilePickerError.fileNotSelected);
    }
  }

Details to reproduce the issue

  1. run the code above in test widgets
  2. The test will fail with the LateInitializationError.

Error Log
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following LateError was thrown running a test:
LateInitializationError: Field '_instance@1894507694' has not been initialized.

When the exception was thrown, this was the stack:
#0 FilePicker._instance (package:file_picker/src/file_picker.dart)
#1 FilePicker.platform (package:file_picker/src/file_picker.dart:37:37)
#2 SXFilePicker.pickCustomFile (package:socxly/common/image_picker.dart:21:49)
#3 OriginalUrlWidget.build. (package:socxly/shortx_create/view/shortx_edit_create_custom_views.dart:132:52)
#4 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:351:24)
#5 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:656:11)
#6 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:313:5)
#7 BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:246:7)
#8 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:703:9)
#9 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:98:12)
#10 PointerRouter._dispatchEventToRoutes. (package:flutter/src/gestures/pointer_router.dart:143:9)
#11 _LinkedHashMapMixin.forEach (dart:_compact_hash:726:13)
#12 PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:141:18)
#13 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:127:7)
#14 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:502:19)
#15 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:482:22)
#16 RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:457:11)
#17 GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:427:7)
#18 GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:390:5)
#19 TestWidgetsFlutterBinding.handlePointerEventForSource. (package:flutter_test/src/binding.dart:677:42)
#20 TestWidgetsFlutterBinding.withPointerEventSource (package:flutter_test/src/binding.dart:687:11)
#21 TestWidgetsFlutterBinding.handlePointerEventForSource (package:flutter_test/src/binding.dart:677:5)
#22 WidgetTester.sendEventToBinding. (package:flutter_test/src/widget_tester.dart:878:15)
#25 TestAsyncUtils.guard (package:flutter_test/src/test_async_utils.dart:74:41)
#26 WidgetTester.sendEventToBinding (package:flutter_test/src/widget_tester.dart:877:27)
#27 TestGesture.up. (package:flutter_test/src/test_pointer.dart:558:26)
#30 TestAsyncUtils.guard (package:flutter_test/src/test_async_utils.dart:74:41)
#31 TestGesture.up (package:flutter_test/src/test_pointer.dart:551:27)
#32 WidgetController.tapAt. (package:flutter_test/src/controller.dart:1096:21)

#33 TestAsyncUtils.guard. (package:flutter_test/src/test_async_utils.dart:120:7)

#34 main.. (file:///Users/hadia/Documents/GitHub/socxly/test/shortx_edit_page_test.dart:796:9)

#35 testWidgets.. (package:flutter_test/src/widget_tester.dart:189:15)

#36 TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:1027:5)


(elided 5 frames from dart:async and package:stack_trace)

The test description was:
renders correctly when file pick in file edit is failed due to excess file size
════════════════════════════════════════════════════════════════════════════════════════════════════

Test failed. See exception logs above.
The test description was: renders correctly when file pick in file edit is failed due to excess file size

Flutter Version details
[✓] Flutter (Channel stable, 3.27.1, on macOS 14.5 23F79 darwin-x64, locale en-US)
• Flutter version 3.27.1 on channel stable at /Users/hadia/Documents/src/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 17025dd882 (3 weeks ago), 2024-12-17 03:23:09 +0900
• Engine revision cb4b5fff73
• Dart version 3.6.0
• DevTools version 2.40.2

[!] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
• Android SDK at /Users/hadia/Library/Android/sdk
✗ cmdline-tools component is missing
Run path/to/sdkmanager --install "cmdline-tools;latest"
See https://developer.android.com/studio/command-line for more details.
✗ Android license status unknown.
Run flutter doctor --android-licenses to accept the SDK licenses.
See https://flutter.dev/to/macos-android-setup for more details.

[✓] Xcode - develop for iOS and macOS (Xcode 16.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 16B40
• CocoaPods version 1.16.1

@hadiabdulla hadiabdulla added the new issue An issue that hasn't yet been seen from the maintainer label Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new issue An issue that hasn't yet been seen from the maintainer
Projects
None yet
Development

No branches or pull requests

1 participant