0

I have some scenes with background and objects (Images). I have the next code to precache Images in flutter:

void didChangeDependencies() {
  
  Imagecahe.clear;

  for (int i = 0; i < widget.scenesList.length; i++) {
    precacheImage(widget.scenesList[i].background.image.image, context).then((_) {
      setState((){
      });
    });

    for (int i1 = 0; i1 < widget.scenesList[i].objetsList.length; i1++) {
      precacheImage(widget.scenesList[i].objetsList[i1].image.image, context).then((_) {
        setState((){
        });
      });
    }
  }
  super.didChangeDependencies();
}

The problem is that sometimes the result is running out of memory and being killed by the operating system.

I've tried clear the image cache scene by scene but doesn't work.

There is some solution that detect hardware's limitations that avoid out of memory?

2 Answers 2

1

You could try to listen for WidgetsBindingObserver's 'didHaveMemoryPressure', and run

imageCache.clear();

(See : https://api.flutter.dev/flutter/painting/imageCache.html)

Here is a full soucer for the WidgetsBindingObserver's implementation

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:theheroesbook/helpers/my_logger.dart';

class LifecycleEventHandler extends WidgetsBindingObserver     
{
  LifecycleEventHandler(
  {this.didPopRouteCallback,
  this.didHaveMemoryPressureCallback,
  this.didChangeTextScaleFactorCallback,
  this.didChangeMetricsCallback,
  this.didPushRouteCallback,
  this.pausedCallBack,
  this.inactiveCallBack,
  this.resumeCallBack,
  this.detachedCallBack});

final void Function(bool)? didPopRouteCallback;
final AsyncCallback? didHaveMemoryPressureCallback;
final AsyncCallback? didChangeTextScaleFactorCallback;
final AsyncCallback? didChangeMetricsCallback;
final void Function(String, bool)? didPushRouteCallback;
final AsyncCallback? pausedCallBack;
final AsyncCallback? inactiveCallBack;
final AsyncCallback? resumeCallBack;
final AsyncCallback? detachedCallBack;

@override
Future<bool> didPopRoute() async {
final ret = await super.didPopRoute();
if (didPopRouteCallback != null) {
  didPopRouteCallback!(ret);
}
return ret;
}

@override
void didHaveMemoryPressure() {
 if (didHaveMemoryPressureCallback != null) {
   didHaveMemoryPressureCallback!();
 }
 super.didHaveMemoryPressure();
}

@override
Future<void> didChangeAppLifecycleState(AppLifecycleState               
state) async {
 switch (state) {
   case AppLifecycleState.inactive:
     {
       if (pausedCallBack != null) {
         await pausedCallBack!();
       }
     }
     break;
   case AppLifecycleState.paused:
     {
       if (inactiveCallBack != null) {
         await inactiveCallBack!();
       }
     }
     break;
   case AppLifecycleState.detached:
     {
       if (detachedCallBack != null) {
         await detachedCallBack!();
       }
     }
     break;
   case AppLifecycleState.resumed:
     if (resumeCallBack != null) {
          await resumeCallBack!();
     }
     break;
 }
 MyLogger().logger.i('$state');
}

@override
void didChangeTextScaleFactor() {
  if (didChangeTextScaleFactorCallback != null) {
    didChangeTextScaleFactorCallback!();
  }
  super.didChangeTextScaleFactor();
}

@override
void didChangeMetrics() {
  if (didChangeMetricsCallback != null) {
    didChangeMetricsCallback!();
  }
  super.didChangeMetrics();
}

@override
Future<bool> didPushRoute(String route) async {
  final ret = await super.didPushRoute(route);
  if (didPushRouteCallback != null) {
    didPushRouteCallback!(route, ret);
  }
  return ret;
}
}
Sign up to request clarification or add additional context in comments.

Comments

0

I found the bug. Turns out I had sprite images for animations that unfolded in a single row generating images up to 4000px width. By distributing the sprite in rows and several columns, I optimized the images with dimensions that did not exceed 1000 px, neither in width nor in height. Now it runs with extraordinary fluidity without the risk of generating excess memory.

[One row

[Various rows

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.