Skip to content

Issue with generic function definition ((String?) => Future<String>' is not a subtype of type '((dynamic) => FutureOr<String?>)?') #125387

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

Closed
2 tasks done
nblum37 opened this issue Apr 23, 2023 · 4 comments
Labels
a: error message Error messages from the Flutter framework a: tests "flutter test", flutter_test, or one of our tests found in release: 3.7 Found to occur in 3.7 found in release: 3.10 Found to occur in 3.10 framework flutter/packages/flutter repository. See also f: labels. has reproducible steps The issue has been confirmed reproducible and is ready to work on r: invalid Issue is closed as not valid

Comments

@nblum37
Copy link

nblum37 commented Apr 23, 2023

Is there an existing issue for this?

Steps to reproduce

Run the code below with Flutter 3.7.11 as a test.

Expected results

It should run without runtime exceptions.

Actual results

The mentioned code will throw a runtime exception at line 68 at the snippet testClass.validator with the message type '(String?) => Future<String>' is not a subtype of type '((dynamic) => FutureOr<String?>)?'. In my production code, I have a List where the specific type of each entry could vary, that's why the first workaround in my sample code is not feasible. The other two workarounds are helping for now but on the one side unnecessary code and additionally are marked by the linter as unnecessary.

Thank you for the support! :)

Code sample

Code sample
import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';

typedef FunctionProto<T> = FutureOr<String?> Function(T? value);

@immutable
class TestClass<T> {
  final FunctionProto<T>? function;

  const TestClass({
    this.function,
  });
}

void main() {
  group('Generic function subtype problem', () {
    test('Infeasible workaround regarding the use case', () async {
      TestClass<String> testClass = TestClass<String>(
        function: (value) async {
          return 'return some value';
        },
      );

      if (testClass.function != null) {
        print('Call function');
        String? err = await testClass.function!('handover some value');
        print('err: $err');
      }
    });

    test('Ugly workaround 1 -> Linter warning due to unnecessary cast', () async {
      TestClass testClass = TestClass<String>(
        function: (value) async {
          return 'test';
        } as FunctionProto<String>,
      );

      if (testClass.function != null) {
        print('Call function');
        String? err = await testClass.function!('handover some value');
        print('err: $err');
      }
    });

    test('Ugly workaround 2 -> Linter warning due to unnecessary cast', () async {
      TestClass testClass = TestClass<String>(
        function: (value) async {
          return 'test';
        } as FunctionProto,
      );

      if (testClass.function != null) {
        print('Call function');
        String? err = await testClass.function!('handover some value');
        print('err: $err');
      }
    });

    test('Main issue', () async {
      TestClass testClass = TestClass<String>(
        function: (value) async {
          return 'test';
        },
      );

      if (testClass.function != null) {
        print('Call function');
        String? err = await testClass.function!('handover some value');
        print('err: $err');
      }
    });
  });
}

Screenshots or Video

Screenshots / Video demonstration

Bildschirm­foto 2023-04-23 um 14 20 28

Logs

No response

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.7.11, on macOS 13.2.1 22D68 darwin-x64, locale de-DE)
[✓] Android toolchain - develop for Android devices (Android SDK version 32.0.0-rc1)
[✓] Xcode - develop for iOS and macOS (Xcode 14.3)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.1)
[✓] VS Code (version 1.44.2)
[✓] Connected device (3 available)
[✓] HTTP Host Availability
@darshankawar darshankawar added the in triage Presently being triaged by the triage team label Apr 24, 2023
@darshankawar
Copy link
Member

Thanks for the detailed report. Using the code sample provided, seeing the same error on latest stable and master.

Testing started at 2:03 pm ...

Call function
err: return some value
Call function
err: test
Call function
err: test
test/widget_test.dart 68:21  main.<fn>.<fn>

type '(String?) => Future<String>' is not a subtype of type '((dynamic) => FutureOr<String?>)?'


stable, master flutter doctor -v
[!] Flutter (Channel stable, 3.7.12, on macOS 12.2.1 21D62 darwin-x64, locale
    en-GB)
    • Flutter version 3.7.12 on channel stable at
      /Users/dhs/documents/fluttersdk/flutter
    ! Warning: `flutter` on your path resolves to
      /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter, which is not inside
      your current Flutter SDK checkout at
      /Users/dhs/documents/fluttersdk/flutter. Consider adding
      /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path.
    ! Warning: `dart` on your path resolves to
      /Users/dhs/Documents/Fluttersdk/flutter/bin/dart, which is not inside your
      current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter.
      Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front
      of your path.
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 4d9e56e694 (3 days ago), 2023-04-17 21:47:46 -0400
    • Engine revision 1a65d409c7
    • Dart version 2.19.6
    • DevTools version 2.20.1
    • If those were intentional, you can disregard the above warnings; however
      it is recommended to use "git" directly to perform update checks and
      upgrades.

[!] Xcode - develop for iOS and macOS (Xcode 12.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    ! Flutter recommends a minimum Xcode version of 13.
      Download the latest version or update via the Mac App Store.
    • CocoaPods version 1.11.2

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

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

[✓] Connected device (5 available)
    • SM G975F (mobile)       • RZ8M802WY0X • android-arm64   • Android 11 (API 30)
    • Darshan's iphone (mobile)  • 21150b119064aecc249dfcfe05e259197461ce23 •
      ios            • iOS 14.4.1 18D61
    • 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 98.0.4758.80

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 1 category.

[!] Flutter (Channel master, 3.10.0-12.0.pre.29, on macOS 12.2.1 21D62
    darwin-x64, locale en-GB)
    • Flutter version 3.10.0-12.0.pre.29 on channel master at
      /Users/dhs/documents/fluttersdk/flutter
    ! Warning: `flutter` on your path resolves to
      /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter, which is not inside
      your current Flutter SDK checkout at
      /Users/dhs/documents/fluttersdk/flutter. Consider adding
      /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path.
    ! Warning: `dart` on your path resolves to
      /Users/dhs/Documents/Fluttersdk/flutter/bin/dart, which is not inside your
      current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter.
      Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front
      of your path.
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 60077ff979 (18 hours ago), 2023-04-23 07:09:16 -0400
    • Engine revision fcf46af2d3
    • Dart version 3.1.0 (build 3.1.0-35.0.dev)
    • DevTools version 2.23.1
    • If those were intentional, you can disregard the above warnings; however
      it is recommended to use "git" directly to perform update checks and
      upgrades.

[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at /Users/dhs/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/docs/get-started/install/macos#android-setup for
      more details.

[✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 13C100
    • CocoaPods version 1.11.2

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

[✓] IntelliJ IDEA Ultimate Edition (version 2021.3.2)
    • IntelliJ at /Applications/IntelliJ IDEA.app
    • Flutter plugin version 65.1.4
    • Dart plugin version 213.7228

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

[✓] Connected device (3 available)
    • Darshan's iphone (mobile) • 21150b119064aecc249dfcfe05e259197461ce23 • ios
      • iOS 15.3.1 19D52
    • macOS (desktop)           • macos                                    •
      darwin-x64     • macOS 12.2.1 21D62 darwin-x64
    • Chrome (web)              • chrome                                   •
      web-javascript • Google Chrome 109.0.5414.119

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

! Doctor found issues in 1 category.
      
[!] Xcode - develop for iOS and macOS (Xcode 12.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    ! Flutter recommends a minimum Xcode version of 13.
      Download the latest version or update via the Mac App Store.
    • CocoaPods version 1.11.2

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

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

[✓] Connected device (5 available)
    • SM G975F (mobile)       • RZ8M802WY0X • android-arm64   • Android 11 (API 30)
    • Darshan's iphone (mobile)  • 21150b119064aecc249dfcfe05e259197461ce23 •
      ios            • iOS 14.4.1 18D61
    • 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 98.0.4758.80

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 1 category.



@darshankawar darshankawar added a: tests "flutter test", flutter_test, or one of our tests framework flutter/packages/flutter repository. See also f: labels. a: error message Error messages from the Flutter framework has reproducible steps The issue has been confirmed reproducible and is ready to work on found in release: 3.7 Found to occur in 3.7 found in release: 3.10 Found to occur in 3.10 and removed in triage Presently being triaged by the triage team labels Apr 24, 2023
@goderbauer
Copy link
Member

This looks like a question for the Dart programming language unrelated to Flutter. You may want to try raising it in the Dart issue tracker here: https://github.com/dart-lang/sdk/issues

@darshankawar
Copy link
Member

Based on above comment, I'll close this from here. Please open it in dedicated repo.

@github-actions
Copy link

github-actions bot commented May 9, 2023

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a: error message Error messages from the Flutter framework a: tests "flutter test", flutter_test, or one of our tests found in release: 3.7 Found to occur in 3.7 found in release: 3.10 Found to occur in 3.10 framework flutter/packages/flutter repository. See also f: labels. has reproducible steps The issue has been confirmed reproducible and is ready to work on r: invalid Issue is closed as not valid
Projects
None yet
Development

No branches or pull requests

3 participants