text stringlengths 1 372 |
|---|
or one of its parent views. |
<topic_end> |
<topic_start> |
performance |
platform views in flutter come with performance trade-offs. |
for example, in a typical flutter app, the flutter UI is composed |
on a dedicated raster thread. this allows flutter apps to be fast, |
as the main platform thread is rarely blocked. |
while a platform view is rendered with hybrid composition, |
the flutter UI is composed from the platform thread, |
which competes with other tasks like handling OS or plugin messages. |
prior to android 10, hybrid composition copied each flutter frame |
out of the graphic memory into main memory, and then copied it back |
to a GPU texture. as this copy happens per frame, the performance of |
the entire flutter UI might be impacted. in android 10 or above, the |
graphics memory is copied only once. |
virtual display, on the other hand, |
makes each pixel of the native view |
flow through additional intermediate graphic buffers, |
which cost graphic memory and drawing performance. |
for complex cases, there are some techniques that |
can be used to mitigate these issues. |
for example, you could use a placeholder texture |
while an animation is happening in dart. |
in other words, if an animation is slow while a |
platform view is rendered, |
then consider taking a screenshot of the |
native view and rendering it as a texture. |
for more information, see: |
<topic_end> |
<topic_start> |
restore state on android |
when a user runs a mobile app and then selects another |
app to run, the first app is moved to the background, |
or backgrounded. the operating system (both iOS and android) |
might kill the backgrounded app to release memory and |
improve performance for the app running in the foreground. |
when the user selects the app again, bringing it |
back to the foreground, the OS relaunches it. |
but, unless you’ve set up a way to save the |
state of the app before it was killed, |
you’ve lost the state and the app starts from scratch. |
the user has lost the continuity they expect, |
which is clearly not ideal. |
(imagine filling out a lengthy form and being interrupted |
by a phone call before clicking submit.) |
so, how can you restore the state of the app so that |
it looks like it did before it was sent to the |
background? |
flutter has a solution for this with the |
RestorationManager (and related classes) |
in the services library. |
with the RestorationManager, the flutter framework |
provides the state data to the engine as the state |
changes, so that the app is ready when the OS signals |
that it’s about to kill the app, giving the app only |
moments to prepare. |
instance state vs long-lived state |
when should you use the RestorationManager and |
when should you save state to long term storage? |
instance state |
(also called short-term or ephemeral state), |
includes unsubmitted form field values, the currently |
selected tab, and so on. on android, this is |
limited to 1 MB and, if the app exceeds this, |
it crashes with a TransactionTooLargeException |
error in the native code. |
<topic_end> |
<topic_start> |
overview |
you can enable state restoration with just a few tasks: |
define a restorationId or a restorationScopeId |
for all widgets that support it, |
such as TextField and ScrollView. |
this automatically enables built-in state restoration |
for those widgets. |
for custom widgets, |
you must decide what state you want to restore |
and hold that state in a RestorableProperty. |
(the flutter API provides various subclasses for |
different data types.) |
define those RestorableProperty widgets |
in a state class that uses the RestorationMixin. |
register those widgets with the mixin in a |
restoreState method. |
if you use any navigator API (like push, pushNamed, and so on) |
migrate to the API that has “restorable” in the name |
(restorablepush, resstorablePushNamed, and so on) |
to restore the navigation stack. |
other considerations: |
providing a restorationId to |
MaterialApp, CupertinoApp, or WidgetsApp |
automatically enables state restoration by |
injecting a RootRestorationScope. |
if you need to restore state above the app class, |
inject a RootRestorationScope manually. |
the difference between a restorationId and |
a restorationScopeId: widgets that take a |
restorationScopeID create a new restorationScope |
(a new RestorationBucket) into which all children |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.