I'm learning flutter concept of widget key by this post. I'm confused by the fact that even there is no key in the two stateless child widgets, still they can swap places correctly. Why? Isn't the key is the only way the parent to track which child is which?(so that it can swap their locations). Please checkout the code in dartpad
In Flutter, why stateless children without keys can still work?
183 Views Asked by John Wang At
1
There are 1 best solutions below
Related Questions in FLUTTER
- Bug report: Issue building flutter on a mac
- Is there a way to control where a Text widget overflow occurs (how many lines)?
- How to save to local storage using Flutter?
- How do you use a TextPainter to draw text?
- Passing command line arguments to a flutter app
- IconButton calling setState during onPressed shows no ripple effect
- What would be a good way for a widget to take 1/3 of the screen?
- How can I test a TextPainter?
- How can I inherit a StatefulWidget's State?
- Life cycle in flutter
- Preloading local image assets in Flutter
- Flutter app not able to run in IntelliJ
- Is there a way to call specific code right before the app is killed or moves to the background?
- How can I layout widgets based on the size of the parent?
- Multi-line TextField in Flutter
Related Questions in STATE
- UI-Router $stateParams With Master Detail Pattern
- MvvmCross on iOS - Strategies for Saving and Restoring State
- Restoring the value of activity member variable not working
- Having trouble saving changes in Dataset to SQL LocalDB
- Inject a template in parent's view with ui-router
- Angular ui.router. Deep nested routes
- Hiding parent state view from child state view in angularjs, best practices?
- State Monad with `put` Function
- Angular JS: how to "load" an individual record from a json dataset?
- Highcharts: automatically give a highlite backgroundcolor to one series item like in the hover state
- How to delete functions of previous instances of controller?
- $state, $stateParams, getting undefined object
- React Native Change State With Unexpected Logging
- How to trigger router's state on page load?
- How to conditionally check the state of an openstack instance
Related Questions in STATELESSWIDGET
- Error in creating a simple randomizer with stateless widget. The instance member can't be accessed in an initializer
- Rebuild same tab with Flutter BottomNavigationBar
- I made changes in stateless widget structure
- Custom Flutter widget to return two widgets for use with CustomScrollView / slivers
- Fixed height in Container is not working in Flutter
- It is safe to use StatelessWidget when the state changes with a Provider?
- i have a problem in flutter i dont have any idea i try all methods
- Flutter - The non-nullable local variable 'newTaskTitle' must be assigned before it can be used
- what is the difference between creating widget using widget class, and creating class widget that extend statelessWidget?
- Trying to build a future context but my build method has an error
- stateless Widget Keeps Rebuilding
- Is it possible to achieve state management between two stateless widgets with no state manager?
- In Flutter, why stateless children without keys can still work?
- Why is a key sometimes not needed for a StatefulWidget?
- Flutter: Mutable fields in stateless widgets
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
While waiting for a better answer, I've kinda figured out what has happened out there(As someone new to flutter, I could be wrong):
In stateless case, when you swap the widgets, three facts occur: (1)the two widget instances do have changed the location in the widget tree, but (2)the corresponding element tree stay still(This is because every corresponding widget types has no change). (3)The only thing changed in the element tree are the references to the widgets from the elements(to reflect the order changes in the widget tree). Since stateless widget update the screen all by the information of itself(no external state), this make the swap correctly shown on the screen.
In the case of statefull without key, when you swap the widgets, things happened identically to the above stateless case, ie., all the three facts occur again. But why there is no swap shown on the screen? That's because for stateful widgets, screen updating depends on external objects(ie., states) associated with the corresponding element in the element tree. So even though the widget instances do swaped locations(blue,red --> red,blue), flutter still paint them as (blue,red) since this is the order in the element tree(which never change).
In the case of stateful with key, only the first fact of the three occurs. The other two are not true anymore. What happened here is that the element tree did changed according to the changes of the widget tree. It achieves this by matching the key and the type on the two sides. After this, the swap shows correctly on the screen, since the elements do change from (blue,red) to (red,blue). Remember, element tree, not the widget tree, is boss here. widget tree is just a set of configuration and element tree is the guy who is in charge of painting on the screen.
There's still a puzzle left to me. In stateful cases without a key, Flutter call the
buildmethod of the state object everytime a swap happens. But with a key, thebuildhas never been called. why? I guess that's because the references of the element tree changed. Ie., every time a reference changed, flutter callbuildto rebuild the state of the corresponding widget. On the other hand, if there is a key, the references never change. So it's no need for flutter to rebuild the state.Looking forward someone can confirm my understanding.