question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Convert to StatefulWidget does not work on widgets with a Generic.

See original GitHub issue

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!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
slightfootcommented, Oct 30, 2019

@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())
    );
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

flutter - Cannot have function with generic parameter in ...
It's because _MyWidgetState doesn't know how to parameterize the type of its parent widget, so when you try to use widget.fn, the runtime...
Read more >
StatefulWidget class - widgets library - Flutter - Dart API docs
A widget that has mutable state. State is information that (1) can be read synchronously when the widget is built and (2) might...
Read more >
How to use same generic type in StatefulWidget and State ...
It gets called from the build() in the State class. That way the State class doesn't care about the generics. I would have...
Read more >
Why Every Flutter Dev Should Care About BuildContext
Flutter allows us to build and rebuild widgets freely, and inexpensively. However, rebuilding a widget does not necessarily equate to the engine ...
Read more >
StatelessWidget class - widgets library - Flutter - Dart API docs
A widget that does not require mutable state. A stateless widget is a widget that describes part of the user interface by building...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found