class Example extends StatefulWidget {
final String string;
Example(this.string);
@override
_ExampleState createState() => _ExampleState();
}
class Example2 extends StatefulWidget {
final String string;
const Example2(Key key, this.string) : super(key: key);
@override
_Example2State createState() => _Example2State();
}
I'm having trouble trying to understand exactly the difference between these two, if there's any.
The only difference is that one accepts keys. Keys are used to give Flutter information about a widget. They can help Flutter remember the scroll position of a widget, or keeping track of state.
Usually, keys are optional, meaning that you don't have to specify them for all your widgets. But depending on your use case, you may eventually need them.
In summary, there is no apparent difference. The only difference is that one contains keys and the other doesn't.
Also, notice how one has const before it. That helps tell Flutter that if the arguments passed to the widget are constant, it doesn't need to rebuild the widget.
Learn more about keys here: https://www.youtube.com/watch?v=kn0EOS-ZiIc