Passing Data to a Stateful Widget in Flutter

0 votes

I'm wondering what the recommended way of passing data to a stateful widget, while creating it, is.

The two styles I've seen are:

class ServerInfo extends StatefulWidget {

  Server _server;

  ServerInfo(Server server) {
    this._server = server;
  }

  @override
    State<StatefulWidget> createState() => new _ServerInfoState(_server);
}

class _ServerInfoState extends State<ServerInfo> {
  Server _server;

  _ServerInfoState(Server server) {
    this._server = server;
  }
}

This method keeps a value both in ServerInfo and _ServerInfoState, which seems a bit wasteful.

The other method is to use widget._server:

class ServerInfo extends StatefulWidget {

  Server _server;

  ServerInfo(Server server) {
    this._server = server;
  }

  @override
    State<StatefulWidget> createState() => new _ServerInfoState();
}

class _ServerInfoState extends State<ServerInfo> {
  @override
    Widget build(BuildContext context) {
      widget._server = "10"; // Do something we the server value
      return null;
    }
}

This seems a bit backwards as the state is no longer stored in _ServerInfoSate but instead in the widget.

Is there a best practice for this?

Mar 10, 2023 in Android by sarit
• 1,830 points
16,872 views

1 answer to this question.

0 votes

The recommended way of passing data to a stateful widget is by using the constructor of the widget to initialize the data and then accessing it via the widget object in the state object.

So, your first example is the recommended approach. It is efficient and follows the standard pattern for passing data in Flutter. The value is stored in the stateful widget and passed to the state object via the constructor.

Here's an example of how to pass data to a stateful widget using the constructor:

class ServerInfo extends StatefulWidget {
  final Server server;

  ServerInfo({Key? key, required this.server}) : super(key: key);

  @override
  _ServerInfoState createState() => _ServerInfoState();
}

class _ServerInfoState extends State<ServerInfo> {
  @override
  Widget build(BuildContext context) {
    // Access widget.server to use the server data
    return Container();
  }
}

In the above example, the data is passed through the constructor of the ServerInfo widget and stored in the server field. This data is then accessed via the widget object in the state object.

Note that you can also pass a key to the stateful widget constructor for more efficient widget updates, but that is not relevant to the question asked.

To know more, join our Flutter Course today.

answered Mar 18, 2023 by pooja

Related Questions In Android

0 votes
1 answer

How to add a ListView to a Column in Flutter?

To add a ListView to a Column ...READ MORE

answered Mar 9, 2023 in Android by pooja
15,493 views
0 votes
0 answers

How to define a circle shape in an Android XML drawable file?

I'm having some trouble locating the Android ...READ MORE

Nov 22, 2022 in Android by Edureka
• 12,690 points
2,320 views
0 votes
0 answers

How to put a <vector> in a <shape> in Android?

I'm trying to make customizable icons in ...READ MORE

Dec 8, 2022 in Android by Edureka
• 12,690 points
879 views
0 votes
1 answer

How to change package name in flutter?

Yes, you can change the package name ...READ MORE

answered Mar 1, 2023 in Android by vishalini
3,209 views
0 votes
1 answer

What is the relation between stateful and stateless widgets in Flutter?

In Flutter, a widget is either stateless ...READ MORE

answered Mar 28, 2023 in Flutter by seena
2,516 views
0 votes
1 answer

Dart_LoadScriptFromKernel: The binary program does not contain 'main'.

Hi@akhtar, You need to add the main() function ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,460 points
3,553 views
0 votes
1 answer

How to install Dart in Windows system?

Hi@akhtar, Dart is the programming language used to code Flutter apps. ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,460 points
1,109 views
0 votes
1 answer

flutter run: No connected devices

Hi@akhtar, To prepare to run and test your ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,460 points
4,116 views
0 votes
1 answer

How can I add a border to a widget in Flutter?

To add a border to a widget ...READ MORE

answered Mar 1, 2023 in Android by vishalini
11,457 views
0 votes
1 answer

Create a rounded button / button with border-radius in Flutter

To create a rounded button with border-radius ...READ MORE

answered Mar 1, 2023 in Android by vishalini
31,606 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP