Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
6a5ef29a
Commit
6a5ef29a
authored
Dec 13, 2019
by
liyuqian
Committed by
Flutter GitHub Bot
Dec 13, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More clarifications on mutating Widget's children (#46725)
parent
d81d9160
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
55 additions
and
3 deletions
+55
-3
framework.dart
packages/flutter/lib/src/widgets/framework.dart
+55
-3
No files found.
packages/flutter/lib/src/widgets/framework.dart
View file @
6a5ef29a
...
...
@@ -1729,9 +1729,61 @@ abstract class MultiChildRenderObjectWidget extends RenderObjectWidget {
/// The widgets below this widget in the tree.
///
/// If this list is going to be mutated, it is usually wise to put [Key]s on
/// the widgets, so that the framework can match old configurations to new
/// configurations and maintain the underlying render objects.
/// If this list is going to be mutated, it is usually wise to put a [Key] on
/// each of the child widgets, so that the framework can match old
/// configurations to new configurations and maintain the underlying render
/// objects.
///
/// Also, a [Widget] in Flutter is immutable, so directly modifying the
/// [children] such as `someMultiChildRenderObjectWidget.children.add(...)` or
/// as the example code below will result in incorrect behaviors. Whenever the
/// children list is modified, a new list object should be provided.
///
/// ```dart
/// class SomeWidgetState extends State<SomeWidget> {
/// List<Widget> _children;
///
/// void initState() {
/// _children = [];
/// }
///
/// void someHandler() {
/// setState(() {
/// _children.add(...);
/// });
/// }
///
/// Widget build(...) {
/// // Reusing `List<Widget> _children` here is problematic.
/// return Row(children: _children);
/// }
/// }
/// ```
///
/// The following code corrects the problem mentioned above.
///
/// ```dart
/// class SomeWidgetState extends State<SomeWidget> {
/// List<Widget> _children;
///
/// void initState() {
/// _children = [];
/// }
///
/// void someHandler() {
/// setState(() {
/// // The key here allows Flutter to reuse the underlying render
/// // objects even if the children list is recreated.
/// _children.add(ChildWidget(key: ...));
/// });
/// }
///
/// Widget build(...) {
/// // Always create a new list of children as a Widget is immutable.
/// return Row(children: List.from(_children));
/// }
/// }
/// ```
final
List
<
Widget
>
children
;
@override
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment