Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does route creation fail for StatefulWidget?

Tags:

flutter

dart

I am trying to create a route with a StatefulWidget.

Error:

I/flutter (23141): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (23141): The following assertion was thrown building
I/flutter (23141): _OverlayEntry-[LabeledGlobalKey<_OverlayEntryState>#3e9ee](dirty, state: _OverlayEntryState#7b90c):
I/flutter (23141): The builder for route "/" returned null.
I/flutter (23141): Route builders must never return null.

Code:

routes:<String,WidgetBuilder>{
        "/":(_)=>new RouteHome(title: 'Flutter Demo Home Page'),
        ...
class RouteHome extends StatefulWidget {
  RouteHome({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

Queries:

  • Why would it not be allowed for a StatefulWidget?

Issue: Hot reload fails for routes if the widget's base class is changed (StatefulWidget <-> StatelessWidget).

like image 656
Vilokan Labs Avatar asked Feb 25 '18 10:02

Vilokan Labs


People also ask

How do subclasses override statefulwidget's methods?

Subclasses should override this method to return a newly created instance of their associated State subclass: The framework can call this method multiple times over the lifetime of a StatefulWidget. For example, if the widget is inserted into the tree in multiple locations, the framework will create a separate State object for each location.

Can a statefulwidget call the state method more than once?

The framework can call this method multiple times over the lifetime of a StatefulWidget. For example, if the widget is inserted into the tree in multiple locations, the framework will create a separate State object for each location.

How does statefulwidget store state?

StatefulWidget instances themselves are immutable and store their mutable state either in separate State objects that are created by the createState method, or in objects to which that State subscribes, for example Stream or ChangeNotifier objects, to which references are stored in final fields on the StatefulWidget itself.

What happens when a statefulwidget is removed from the tree?

Similarly, if a StatefulWidget is removed from the tree and later inserted in to the tree again, the framework will call createState again to create a fresh State object, simplifying the lifecycle of State objects.


1 Answers

Hot reload is to be used for minimal UI changes only.

Courtesy: "It is not an issue, use hot reload for minimal changes in the UI. Usually you need to do a full restart when you create new classes." – @aziza

like image 80
Vilokan Labs Avatar answered Nov 26 '22 13:11

Vilokan Labs