text
stringlengths 1
372
|
|---|
];
|
<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>
|
<topic_start>
|
how it works
|
when hot reload is invoked, the host machine looks
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.