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?
184 views Asked by John Wang At
1
There are 1 answers
Related Questions in FLUTTER
- Flutter + Dart: Editing name of a tab shows up a black screen
- The Binary Version Of its metadata is 1.8.0, expected Version is 1.6.0 build error
- Way to get CustomPainter to track face in Camera Flutter MLKit
- flutter Null check error: did not show file and line number
- Creating multiple instances of a class with different initializing values in Flutter
- I want to paste stickers into to my TextField and to show the stickers beside the emojis
- Flutter plugin development android src not opening after opening example
- Module not found when building flutter app for IOS
- How to make barrier area interactive in flutter modal bottom sheet
- Can an RPC result be included in a Supabase select function in Flutter for Data Modeling?
- Why do I need to wait to reaccess to Firestore database even though it has already done before?
- Flutter web app on Windows -how to support mouse drag for horizontal and vertical scrolling as well as using mouse wheel
- I wrote this time displaying FLUTTER app, How can I improve it?
- Appwrite and / or Spring Boot Backend
- Flutter two_dimensional_scrollables Web app Chrome - cannot get horizontal scroll to work?
Related Questions in STATE
- NextJS 13 14, Store server state globally
- My state is undefined despite being setted
- Next.js carousel component loads instagram basic display api perfectly except in Safari browser
- How can I create a javascript animation that recognizes when it hits things, in other words a hit box?
- Flutter - When an element is deleted from the ListView, the state of the element under deletion is reset
- State Transfer from a model using Riverpod flutter
- React components don't re render when the state is changed
- Flutter | Persisting Navigator State Using Hydrated Bloc with the Bloc Design Pattern
- Wait set state finishes to call another function
- keyboard focus with GTK4 Rust does not work as expected and how to get state
- Bind state to context value in React JS
- I have a problem when I run on the web Bad state: databaseFactory not initialized databaseFactory
- How to manage state in a chrome extension in this scenario
- Should I use immutable or mutable state for a JSON tree editor in Flutter?
- React Component doesn't change states until file is resaved, then runs both at same time
Related Questions in STATELESSWIDGET
- Why is the value of a variable "ButtonCount" declared before the build method not retaining its value in flutter
- How can Error in flutter stateless widget?
- Is using a StatelessWidget without a const constructor better than using a StatefulWidget?
- I made changes in stateless widget structure
- Why is IconButton a stateless widget in Flutter, rather than being a stateful widget?
- Flutter Stateless Widget initialize values by keeping the constructor as "const"
- transter data from statefull widget to another
- Are there any problems with this use of a Provider instead of a StatefulWidget?
- StatelessWidget gets rebuilt even though parameters stay the same; notifyListeners() with ChangeNotifier
- How to fix `Do not use BuildContexts across async gaps` in a StatelessWidget?
- How to use a method in a Stateful Widget from another Widget
- Flutter : DefaultTabController Tabbar Tab Color not changing on selection
- Flutter Bottomsheet reload problem after dismissing keyboard
- draggableScrollableController not attached to a sheet after it has been attached
- Main app widget: setState call -> build() builds all widgets but screen not updated
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?
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)
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.