Reputation: 57
I have a data cache. It is expensive to fetch some of the data.. other data is quite disposable. The data can be quite large and could conceivably cause the OS to ask apps to free memory.
Android has onTrimMemory() and IOS has applicationDidReceiveMemoryWarning(). Is there a flutter equivalent?
Upvotes: 4
Views: 1746
Reputation: 1760
The widget must implement the WidgetsBindingObserver
and override didHaveMemoryPressure
, like in the following example:
class _HomePageState extends BaseState<HomePage> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
super.dispose();
WidgetsBinding.instance.removeObserver(this);
}
@override
void didHaveMemoryPressure() {
print('didHaveMemoryPressure');
}
}
Upvotes: 5
Reputation: 57
The Flutter WidgetsBindingObserver class provides a method to detect low memory, as demonstrated in this article.
Upvotes: 0