You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Proposal: initialize fields at the declaration site #153644
I thought it was this way because of some code path where the state object could be created. But never actually mounted. Therefore would not be dispose()ed. However I think you're right upon looking into it.... this should be safe
Yeah, forgot to mention that, but it's a good point—there was one time when I tried subscribing to a Firebase stream using a late final variable, and never actually subscribed to it 😒
But recently I learned that I'd missed something important—there are actually 2 potential dangers involving late fields.
late initialization errors
Happens when a late variable is referenced before being initialized; can be avoided by initializing at the declaration site.
calling createTicker() inside dispose()
AnimationController calls vsync.createTicker() in its constructor, so if a late controller isn't referenced until it's being disposed of, it can result in an error.
For example, you could have a method that references the controller, but the controller won't be initialized until if/when the method is called:
This issue is assigned to @nate-thegrate but has had no recent status updates. Please consider unassigning this issue if it is not going to be addressed in the near future. This allows people to have a clearer picture of what work is actually planned. Thanks!
This issue was assigned to @nate-thegrate but has had no status updates in a long time. To remove any ambiguity about whether the issue is being worked on, the assignee was removed.
Activity
moffatman commentedon Aug 18, 2024
I thought it was this way because of some code path where the state object could be created. But never actually mounted. Therefore would not be dispose()ed. However I think you're right upon looking into it.... this should be safe
jonahwilliams commentedon Aug 19, 2024
This is not always a safe change. late finals are not initialized until accessed. Simple example, the following code will never update.
nate-thegrate commentedon Aug 19, 2024
Yeah, forgot to mention that, but it's a good point—there was one time when I tried subscribing to a Firebase stream using a
late finalvariable, and never actually subscribed to it 😒`_InteractiveViewerState` code cleanup (#153645)
nate-thegrate commentedon Aug 23, 2024
I wanted to double-check as well 🙂
Fortunately, it does look like every
Stateobject is mounted when it's created:flutter/packages/flutter/lib/src/widgets/framework.dart
Lines 4483 to 4488 in 1633c47
But recently I learned that I'd missed something important—there are actually 2 potential dangers involving
latefields.late initialization errors
Happens when a
latevariable is referenced before being initialized; can be avoided by initializing at the declaration site.calling
createTicker()insidedispose()AnimationControllercallsvsync.createTicker()in its constructor, so if alatecontroller isn't referenced until it's being disposed of, it can result in an error.For example, you could have a method that references the controller, but the controller won't be initialized until if/when the method is called:
`_InteractiveViewerState` code cleanup (flutter#153645)
flutter-triage-bot commentedon Feb 1, 2025
This issue is assigned to @nate-thegrate but has had no recent status updates. Please consider unassigning this issue if it is not going to be addressed in the near future. This allows people to have a clearer picture of what work is actually planned. Thanks!
flutter-triage-bot commentedon Mar 10, 2025
This issue was assigned to @nate-thegrate but has had no status updates in a long time. To remove any ambiguity about whether the issue is being worked on, the assignee was removed.
late finalfield causes errors #185247 as a duplicate of this issueUpdate TickerMode.getValuesNotifier to handle initialization during S…