B. Whipple
B. Whipple

Reputation: 57

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?

Upvotes: 4

Views: 1746

Answers (2)

Alberto M
Alberto M

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

B. Whipple
B. Whipple

Reputation: 57

The Flutter WidgetsBindingObserver class provides a method to detect low memory, as demonstrated in this article.

Upvotes: 0

Related Questions