1

So I believe Key in Flutter is still something many of us either don't use at all, or use it without knowing what it actually is.

So I was learning some about it and It seems like we don't need / we don't have any use of any key if we have stateless widgets in the Widget Tree.

But When I looked at the source code of StatelessWidget, it seems like it has a Key optional parameter in its constructor.

const StatelessWidget({Key key}) : super(key: key);

But what's the use of Key in Stateless widget?

nvoigt
  • 75,013
  • 26
  • 93
  • 142
erluxman
  • 18,155
  • 20
  • 92
  • 126

1 Answers1

0

checkout this video: https://www.youtube.com/watch?v=kn0EOS-ZiIc

Actually you don't need key if all the widgets in your tree are stateless widget.

But If you use stateless widget surrounding a stateful widget which uses a key, you should move that key to the stateless widget. Check the video for more detail~

Ypy
  • 56
  • 5