Application crashes when using CachedNetworkImage - flutter

Application crashes when using CachedNetworkImage in listview. If there are a lot of pictures, then the application freezes and crashes. Is it possible to load each image one by one, asynchronously? Can I have a code example? Thanks.

And so, if you have the same problem as me, and you have a large list of pictures, then you can do the following:
1.Enable ListView pagination. So that when scrolling, not the entire ListView is loaded, but for example only 10 items from the List.
2.Compress pictures to the desired size using the following parameters:
memCacheWidth, memCacheHeight, maxHeightDiskCache, maxWidthDiskCache
Here is my example:
CachedNetworkImage(
memCacheWidth: 45,
memCacheHeight: 60,
maxHeightDiskCache: 60,
maxWidthDiskCache: 45,
imageUrl: imageUrl,
imageBuilder: (context, imageProvider) => imageBuilderWidget(imageProvider),
placeholder: (context, url) => placeholderWidget(),
errorWidget: (context, url, error) => errorWidget(),
);
After adding these options, remove app from the emulator and do a flutter clean

if need to cache images Use Opt imised cached image package. I had the same issue this package solved. if not working change width of images, add pagination if possible

You can use Image.network, it solved my crash issue on IOS.
Image.network('https://example.com/image.jpg',
errorBuilder: (context, error, stackTrace) {
print(error); //do something
},
loadingBuilder: (context, Widget child,
ImageChunkEvent loadingProgress) {
if (loadingProgress == null) return child;
return Center(
child: CircularProgressIndicator(
value: loadingProgress.expectedTotalBytes != null
? loadingProgress.cumulativeBytesLoaded /
loadingProgress.expectedTotalBytes
: null,
),
);
},
),

Related

FutureBuilder with Icon placeholder flickers

I'm using a FutureBuilder to load album thumbnails into a ListTile, as follows:
ListTile _albumTile(Album album, BuildContext context) {
return ListTile(
leading: FutureBuilder<Uint8List>(
future: AndroidContentResolver.instance.loadThumbnail(
uri: album.thumbnailUri,
width: 56,
height: 56,
),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Image.memory(snapshot.data!);
} else {
return const FittedBox(
fit: BoxFit.contain, child: Icon(Icons.album, size: 56));
}
}),
title: Text(album.album),
subtitle: Text(album.artist),
);
}
But, as the image loads, the placeholder icon is replaced with a blank image, and then with the final thumbnail. The blank image has the wrong width, which results in the ListTile title and subtitle jumping around, causing a flicker.
The following sequence of screenshots shows three consecutive frames:
Image placeholders.
Some of the futures have completed, and are showing thumbnails. The other tiles have a blank image (and the text jumps to the left).
All of the futures have completed.
Even when I fix the text jumping around -- by specifying fit: BoxFit.contain, width: 56, height: 56 for the image -- I still get a flash of white before the thumbnail appears.
What am I doing wrong?
Use precacheImage function to prefetch an image into the image cache.
If the image is later used by an Image or BoxDecoration or FadeInImage, it will probably be loaded faster.
I solved it by using frameBuilder. The documentation says:
If this is null, this widget will display an image that is painted as soon as the first image frame is available (and will appear to "pop" in if it becomes available asynchronously).
This describes exactly what I'm seeing.
It continues with the following:
Callers might use this builder to add effects to the image (such as fading the image in when it becomes available) or to display a placeholder widget while the image is loading.
...and the attached example shows how to implement a fade-in effect. For simplicity, however, I opted for the following:
return Image.memory(
snapshot.data!,
frameBuilder: (context, child, frame, wsl) {
if (frame != null) {
return child;
} else {
return _albumThumbnailPlaceholder();
}
},
Widget _albumThumbnailPlaceholder() {
return FittedBox(fit: BoxFit.contain, child: Icon(Icons.album, size: 56));
}
This seems to completely get rid of the blank frame.

How to Set the Loading Value for the Circular progresss Indicator like progress percentage for 60 days in flutter?

How to Set the loading value for the Circular progresss indicator like progress percentage in flutter for 60 days-Math formula?I need this stuff for my flutter project,Sorry if i am asking something silly:-)
Note: I'm new to this amazing flutter development.
You can set "completed" rate of a progress indicator like this:
CircularProgressIndicator(value: 0.5)
The above will result in a 50% indicator. All you have to do is use a member as value in a StatefulWidget and use setState to update it according to the desired percentage.
Also you can use it to indicate the progress when loading an image:
Image.network(
'<url>',
height: 50,
loadingBuilder: (BuildContext context, Widget child,
ImageChunkEvent? loadingProgress) {
if (loadingProgress == null) {
return child;
}
return Center(
child: CircularProgressIndicator(
value: loadingProgress.expectedTotalBytes != null
? loadingProgress.cumulativeBytesLoaded /
loadingProgress.expectedTotalBytes!
: null,
),
);
},
)

Two StreamBuilderon on one screen with default empty screen shown when neither has data

I'm trying to create a Split View with two ListViews, one of them showing Tasks which are not completed and the second one containing completed tasks. I managed to make this work with a Column containing two Streambuilder. The problem I don't know how to solve is how to show a single default empty screen when neither of the two Streams have any values, due to the way Flutter works, I can't just return null. So I end up having two Empty default screens in my Column and on my screen.
What would be the right approach to make something like shown in the GIF with Firestore?
If I need to work with a single Stream and filter it inside dart, how can I make it work with ListView ?
I'd highly appreciate an example.
My Job class contains a boolean value jobDone which tells me in which list the Job has to go.
My current code:
return Column(
children: [
StreamBuilder<List<Job>>(
stream: getPendingJobsStream(database),
builder: (context, snapshot) {
return ListItemsBuilder<Job>(
...
);
},
),
ExpansionTile(
title: Text('Completed Tasks'),
children: [
StreamBuilder<List<Job>>(
stream: getCompletedJobsStream(database),
builder: (context, snapshot) {
return ListItemsBuilder<Job>(
...
);
},
),
],
),
],
);
You can check for snapshot state
builder: (context, snapshot) {
if(snapshot.connectionState ==ConnectionState.waiting){
return Center(child:CircularProgressIndicator();
}
if(snapshot.connectionState ==ConnectionState.done){
//check if snapshot has data
if(snapshot.hasData){
return ListItemsBuilder<Job>( ..

Select source image resolution dynamically

I'm building an app where the same image can be displayed in a lot of different sizes (big on desktop/tablet and smaller on phones). I also want to take into account the device pixel density to always load the most appropriate source.
I know this is possible in HTML using the srcset and sizes attributes to specify sources and sizes but how would I do something similar in Flutter?
Try AspectRatio, this is responsive for all sizes
AspectRatio(
aspectRatio: 4 / 3, //change value here
child: new Container(
color: Colors.red,
),
),
You can get the display size of the image at the time of build using a LayoutBuilder and the device pixel density using MediaQueries.
I created a package that does just that : https://pub.dev/packages/responsive_image
Just specify your sources and the widget will take care of selecting the most appropriate source and render the image.
ResponsiveImage(
srcSet: {
64: "https://via.placeholder.com/64",
128: "https://via.placeholder.com/128",
256: "https://via.placeholder.com/256",
512: "https://via.placeholder.com/512",
},
scalePreference: ScalePreference.Upper,
);
You can also use the builder to further customize the display of the image (for example using cached_network_image)
ResponsiveImage(
srcSet: {
256: "https://via.placeholder.com/256",
512: "https://via.placeholder.com/512",
1024: "https://via.placeholder.com/1024",
},
builder: (BuildContext context, String url) {
return CachedNetworkImage(
imageUrl: url,
placeholder: (context, url) => CircularProgressIndicator(),
errorWidget: (context, url, error) => Icon(Icons.error),
);
},
);

Flutter : set default image in Image.file

I'm currently creating Updating Image process in Flutter app.
I already succeed getting image that pass from some widget defined in View.dart to UpdateScreen widget defined in Update_screen.dart by using Network Image.
CircleAvatar(
radius: 100,
backgroundImage: NetworkImage(
'${Urls.BASE_API_IMAGE}/${widget.imageUpdate}'),
),
My question is, how to set default image using Image.file() ? , because I must use Image.file() for updating data.
Thank's
My full source code :
My Full Source Code Here
you can use this package cached_network_image as it makes it simple to set a default image.
Example
CachedNetworkImage(
imageUrl: "your url",
placeholder: (context, url) =>Your default Image Widget(),
errorWidget: (context, url, error) => new Icon(Icons.error),
),