text
stringlengths 1
474
|
|---|
children: [Text('T1')],
|
)
|
],
|
),
|
Table(
|
children: const [
|
TableRow(
|
children: [Text('T2')],
|
)
|
],
|
),
|
Table(
|
children: const [
|
TableRow(
|
children: [Text('T3')],
|
)
|
],
|
),
|
Table(
|
children: const [
|
TableRow(
|
children: [Text('T10')], // modified
|
)
|
],
|
),
|
];<code_end>
|
You hot reload, but the change is not reflected.Conversely, in the following example:
|
<code_start>const foo = 1;
|
final bar = foo;
|
void onClick() {
|
print(foo);
|
print(bar);
|
}<code_end>
|
Running the app for the first time prints 1 and 1.
|
Then, you make the following change:
|
<code_start>const foo = 2; // modified
|
final bar = foo;
|
void onClick() {
|
print(foo);
|
print(bar);
|
}<code_end>
|
While changes to const field values are always hot reloaded,
|
the static field initializer is not rerun. Conceptually,
|
const fields are treated like aliases instead of state.The Dart VM detects initializer changes and flags when a set
|
of changes needs a hot restart to take effect.
|
The flagging mechanism is triggered for
|
most of the initialization work in the above example,
|
but not for cases like the following:
|
<code_start>final bar = foo;<code_end>
|
To update foo and view the change after hot reload,
|
consider redefining the field as const or using a getter to
|
return the value, rather than using final.
|
For example, either of the following solutions work:
|
<code_start>const foo = 1;
|
const bar = foo; // Convert foo to a const...
|
void onClick() {
|
print(foo);
|
print(bar);
|
}<code_end>
|
<code_start>const foo = 1;
|
int get bar => foo; // ...or provide a getter.
|
void onClick() {
|
print(foo);
|
print(bar);
|
}<code_end>
|
For more information, read about the differences
|
between the const and final keywords in Dart.<topic_end>
|
<topic_start>
|
Recent UI change is excluded
|
Even when a hot reload operation appears successful and generates no
|
exceptions, some code changes might not be visible in the refreshed UI.
|
This behavior is common after changes to the app’s main() or
|
initState() methods.As a general rule, if the modified code is downstream of the root
|
widget’s build() method, then hot reload behaves as expected.
|
However, if the modified code won’t be re-executed as a result
|
of rebuilding the widget tree, then you won’t
|
see its effects after hot reload.For example, consider the following code:
|
<code_start>import 'package:flutter/material.dart';
|
void main() {
|
runApp(MyApp());
|
}
|
class MyApp extends StatelessWidget {
|
const MyApp({super.key});
|
@override
|
Widget build(BuildContext context) {
|
return GestureDetector(onTap: () => print('tapped'));
|
}
|
}<code_end>
|
After running this app, change the code as follows:
|
<code_start>import 'package:flutter/widgets.dart';
|
void main() {
|
runApp(const Center(child: Text('Hello', textDirection: TextDirection.ltr)));
|
}<code_end>
|
With a hot restart, the program starts from the beginning,
|
executes the new version of main(),
|
and builds a widget tree that displays the text Hello.However, if you hot reload the app after this change,
|
main() and initState() are not re-executed,
|
and the widget tree is rebuilt with the unchanged instance
|
of MyApp as the root widget.
|
This results in no visible change after hot reload.<topic_end>
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.