Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can a Flutter application receive low memory notifications?

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?

like image 931
B. Whipple Avatar asked Oct 28 '25 15:10

B. Whipple


1 Answers

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');
  }

}
like image 79
Alberto M Avatar answered Oct 31 '25 10:10

Alberto M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!