When to Use Keys - Flutter Widgets 101 Ep. 4
Why Stack Overflow recommends it
Stack Overflow users link it when a Flutter list or stateful widget keeps stale state or reorders wrongly, and the fix is adding the right Key.
Recommended by 48 people across 33 different months, 2018 to 2025.
What it is
A 9m40s episode of Google's Flutter Widgets 101 series explaining the key parameter on widgets. It covers when keys are needed, how Flutter matches widgets and state in the tree, where to place keys, and the different key types such as ValueKey, ObjectKey, UniqueKey and GlobalKey.
What people said
Strong rec d0xzen · 2019-05 · Stack Overflow · on “What are Keys in the Stateless widgets class?”
The Key is an optional parameter needed to preserve state in your widget tree, you have to use them if you want to move a collection of elements in your tree and preserve the state of them. The best explanation can be found in this video by Google When to Use Keys - Flutter Widgets 101 Ep. 4 [this video]
Strong rec mavini.s · 2023-01 · Stack Overflow · on “Flutter - setState not updating inner Stateful Widget”
The real issue on child StatefulWidget not rebuilding is in the KEY Hey, I'm a bit late to the discussion, but I think this is important. I was facing a similar problem a while back and I even came to this thread to get some ideas. In my case, I was simply getting widget.value directly inside the build method of the childWidget, and it was not updating when i called setState in the mainWidget. Then i found this video: [this video] (When to Use Keys - Flutter Widgets 101 Ep. 4) - Here the Google dev talks about how keys in Flutter. The short answer is In a StatefulWidget the actual value you pass is stored in the state, not in the widget itself, like a StatelessWidget does. When you call setState
Recommends Miguel Ruivo · 2019-02 · Stack Overflow · on “Force Stack to recreate custom widget”
First things first. When you have the same widgets on a list, Flutter won't know if they've changed order or not, that's where the key may help you. To see how to use the keys you may want to check here [LINK] and this video [this video] that explains quite well for list scenarios where you want to re-order an item, for example. A setState will call the build in the widgets state present already in the tree, but won't call the constructor or initState , those are only when the widget is actually inserted in the tree. When the parent changes, Flutter will look for changes on its children and call the didUpdateWidget method on each widget state where you can override and actually compare the change
Recommends Danny Tuppeny · 2019-03 · Stack Overflow · on “what does super and Key do in flutter?”
super is used to call the constructor of the base class. So in your example, the constructor of CardTitle is calling the constructor of StatelessWidget . Key is a type used in Flutter to identify widgets and allows Flutter to know when a widget that's moved in the tree is the same as a widget that was previously in a different location. There's a good video about keys here: [this video]
Recommends abdalmonem · 2019-03 · Stack Overflow · on “Flutter ListView.builder on remove element not updating list”
you should add a key to the returned widget of your ListView builder i recommend you to see this video from google devolopers channel : [this video] it well help you to understand more about "keys"
Recommends loushou · 2019-05 · Stack Overflow · on “ListVIew doesn't scroll to the correct offset position after its built again”
PageStorage holds key value pairs for all sorts of data on many Flutter core widgets. You can even leverage it for your own widgets. It is all based on the Key of the widget though. Think of it as global storage space for data specific to every Stateful widget. It allows you to persist data for widgets, even through complete widget destruction, as long as you use the same Key. In many ways, it is like Flutter's internal state-management. Here is a good video about Keys [this video]
Recommends Crazy Lazy Cat · 2019-12 · Stack Overflow · on “Deleting item from Flutters ListView removes the last widget only”
You should set Key for each Tile. You can watch this Flutter Tutorial [this video] to understand what is going on in your code. I used ObjectKey in the following code. import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Slidable Demo', home: MyListPage(), ); } } class MyListPage extends StatefulWidget { MyListPage({Key key}) : super(key: key); @override _MyListPageState createState() => _MyListPageState(); } class _MyListPageState extends State<MyListPage> { final List<Item> items = [ Item("ItemName_1", "ItemType_1"), Item("ItemName_2", "ItemType_2"), Item("
Recommends pr0gramista · 2019-12 · Stack Overflow · on “Preserving state across pages in Flutter”
You can use GlobalKey [LINK] to preserve the same state across different pages. class RandomColor extends StatefulWidget { RandomColor({Key key}) : super(key: key); // need this @override State<StatefulWidget> createState() => RandomColorState(); } Here you have a great Google Developers video [this video] about keys. You may also consider using some kind of state managment [LINK] .
Recommends Haidar · 2020-02 · Stack Overflow · on “SlideAnimation Not Starting Again After Calling SetState()”
I figured it out, the reason is in the keys and widget trees as explained in this [this video] video. When the setState was called without CustomSlideTransition having a unique key, then the framework did not recreate the State object of this class again and it instead used the State object of the previous widget which had the AnimationStatus as completed But now with keys each time the framework creates a new State object and calls initState() where the controller is reset and the animation starts normally. I also found a workaround but I don't know if it is as efficient as the "keys" solution: Without adding a key to the CustomSlideTransition widget,I can, in the build() method of this widget,
Recommends Andrey Ozornin · 2020-06 · Stack Overflow · on “How to copy the value of a Widget Text to another using onPressed property of a button”
Answering your question directly: you can access text inside Text widget using its data [LINK] property. Text widget = Text('text value'); String text = widget.data; print(text); // text value Next, you can't access widgets by their key properties. At least you shouldn't, because they were designed for different purpose: here's a video [this video] and an article [LINK] about keys in Flutter. What you can do here is turn your TextWidget from StatelessWidget into StatefulWidget and render contents of your second Text based on the state. Good introduction into what the state is and why you should use it can be found on official Flutter website: Start thinking declaratively [LINK] . Then you can sav