Skip to content

Type error in generic closure #42763

Closed
Closed
@tvolkert

Description

@tvolkert

Steps to reproduce

import 'package:flutter/widgets.dart';

typedef ValueCallback<T> = void Function(T value);

void main() {
  runApp(
    BugReport<String>(
      onValue: (String value) {
        print(value);
      },
    ),
  );
}

class BugReport<T> extends StatefulWidget {
  const BugReport({
    @required this.onValue,
  });

  final ValueCallback<T> onValue;

  @override
  _BugReportState<T> createState() => _BugReportState<T>();
}

class _BugReportState<T> extends State<BugReport> {
  @override
  void initState() {
    super.initState();
    print(widget.onValue);  // <----- This should be fine, but it throws
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

Expected behavior

You expect no errors in running this code

Actual behavior

The following error is thrown:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following _TypeError was thrown attaching to the render tree:
type '(String) => Null' is not a subtype of type '(dynamic) => void'

When the exception was thrown, this was the stack:
#0      _BugReportState.initState (package:bugreport/main.dart:32:18)
#1      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4695:58)
#2      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4531:5)
#3      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3501:14)
#4      Element.updateChild (package:flutter/src/widgets/framework.dart:3260:18)
#5      RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1217:16)
#6      RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1188:5)
#7      RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1130:17)
#8      BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2620:19)
#9      RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1129:13)
#10     WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:941:7)
#11     WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:922:7)
(elided 11 frames from class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch)
════════════════════════════════════════════════════════════════════════════════════════════════════

Other information

[✓] Flutter (Channel master, 1.21.0-2.0.pre.55, on Mac OS X 10.15.3 19D76, locale en-US)
    • Flutter version 1.21.0-2.0.pre.55 at /Users/tvolkert/project/flutter/flutter
    • Framework revision 1840b7121a (3 days ago), 2020-07-16 23:15:23 -0700
    • Engine revision d3b81f19fc
    • Dart version 2.9.0 (build 2.9.0-21.0.dev 06183779d7)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions