Ask Your Question
4

Is there a replacement for initState() in the Provider ecosystem?

asked 2023-01-10 11:00:00 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-10-29 08:00:00 +0000

huitzilopochtli gravatar image

Yes, in the Provider ecosystem, the recommended replacement for initState() is to use the Provider package's "create" method to initialize your state. This method is called during the widget's creation, and allows you to set up your state as soon as possible. Here is an example of how to use the create method to initialize your state:

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => MyState(),
      child: Text("My Widget"),
    );
  }
}

In this example, the create method is used to create a new instance of the MyState class, which extends the ChangeNotifier class. This provides the widget with access to the state of the MyState object, which can be accessed with a Consumer widget.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-01-10 11:00:00 +0000

Seen: 21 times

Last updated: Oct 29 '22