Flutter-intellij: Convert to StatefulWidget does not work on widgets with a Generic.

Created on 30 Oct 2019  ·  5Comments  ·  Source: flutter/flutter-intellij

Steps to Reproduce

Performing the quick-action whilst hovering over StatelessWidget in the following example will offer you to "Convert to StatefulWidget >".

class HelloWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return const Text('world');
  }
}

However in this example with the Generic T we loose that quick-action option.

class HelloWidget<T> extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return const Text('world');
  }
}

Version info

[√] Flutter (Channel dev, v1.10.14, on Microsoft Windows [Version 10.0.17763.805], locale en-GB)
    • Flutter version 1.10.14 at C:\Android\flutter
    • Framework revision 1946fc4da0 (3 weeks ago), 2019-10-07 15:23:31 -0700
    • Engine revision 1d62160fdb
    • Dart version 2.6.0 (build 2.6.0-dev.5.0 d6c6d12ebf)

[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at C:\Android\SDK
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-29, build-tools 29.0.2
    • ANDROID_HOME = C:\Android\SDK
    • Java binary at: C:\Android\Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[√] Android Studio (version 3.5)
    • Android Studio at C:\Android\Studio
    • Flutter plugin version 39.0.3
    • Dart plugin version 191.8423
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)

[√] IntelliJ IDEA Ultimate Edition (version 2019.2)
    • IntelliJ at C:\Users\Simon\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\192.6817.14
    • Flutter plugin version 39.0.4
    • Dart plugin version 192.6527

[√] VS Code, 64-bit edition (version 1.27.2)
    • VS Code at C:\Program Files\Microsoft VS Code
    • Flutter extension version 2.18.0

[√] Connected device (3 available)
    • iPhone XR, 9 0, API 28 0 828x1792 • 192.168.188.109:5555 • android-x86    • Android 9 (API 28)
    • Chrome                            • chrome               • web-javascript • Google Chrome 77.0.3865.120
    • Headless Server                   • headless-server      • web-javascript • Flutter Tools

• No issues found!
dart sdk topic-editing

Most helpful comment

@pq yes, it has to propagate to the State.

Example:

typedef Fetcher<T> = T Function(BuildContext context);

class MyWidget<T> extends StatefulWidget {
  const MyWidget({
    Key key,
    @required this.fetcher,
  })  : assert(fetcher != null),
        super(key: key);

  final Fetcher<T> fetcher;

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

class _MyWidgetState<T> extends State<MyWidget<T>> {
  T _value;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    _value = widget.fetcher(context);
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text(_value.toString())
    );
  }
}

All 5 comments

Thanks for the report @slightfoot!

I've got this reproduced locally and it looks like an issue with the analysis server assist.

Cheers @slightfoot! We were looking at the same time. 👍

I've got a provisional fix up for review: https://dart-review.googlesource.com/c/sdk/+/123507.

Open question: should the type param propagate to the State? That is:

class _MyWidgetState<T> extends State<MyWidget<T>>

Currently the assist will produce:

class _MyWidgetState extends State<MyWidget>

@pq yes, it has to propagate to the State.

Example:

typedef Fetcher<T> = T Function(BuildContext context);

class MyWidget<T> extends StatefulWidget {
  const MyWidget({
    Key key,
    @required this.fetcher,
  })  : assert(fetcher != null),
        super(key: key);

  final Fetcher<T> fetcher;

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

class _MyWidgetState<T> extends State<MyWidget<T>> {
  T _value;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    _value = widget.fetcher(context);
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text(_value.toString())
    );
  }
}
Was this page helpful?
0 / 5 - 0 ratings