I have a Flutter app, that is crashing because of images, so I install some plugins to test, after some study, I found a solution for my problem with memory in Flutter. It is setting the cache a Heigth and a Width because the image that comes from the server I did not put a good resize coding and some clients upload large images in dimensions
Image.network(
MainConfig.storageDoc + imageFeature,
cacheHeight: constraints.maxHeight.toInt(),
cacheWidth: constraints.maxWidth.toInt(),
height: constraints.maxHeight,
width: constraints.maxWidth,
fit: BoxFit.contain,
)
the cacheHeight and cacheWith from this save me from memory, now my app is fast but the image is POOR, how can I resize and maintain quality?
I solved the problem and I will post here what is the solution to make a good performance for your app.
After some searches on the web, I found that all the system on the backend that has an upload function for the user upload the image you must make 3 dimensions image.
image-large image-medium image-small
And the following dimensions are respectively 1024, 600 and 300. I use PHP to format all my images and now my app is flying.
This is the helper function that I made and is working great
static String formatImage(String url, [kSize size = kSize.medium]) {
if (Config().isCacheImage ?? kAdvanceConfig['kIsResizeImage']) {
String pathWithoutExt = p.withoutExtension(url);
String ext = p.extension(url);
String imageURL = url ?? kDefaultImage;
if (ext == ".jpeg") {
imageURL = url;
} else {
switch (size) {
case kSize.large:
imageURL = '$pathWithoutExt-large$ext';
break;
case kSize.small:
imageURL = '$pathWithoutExt-small$ext';
break;
default: // kSize.medium:e
imageURL = '$pathWithoutExt-medium$ext';
break;
}
}
return imageURL;
} else {
return url;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With