In my application, I use these 2 classes but I don't know which one I should prioritize.
Image.asset('icons/heart.png')
AssetImage('icons/hear.png')
Maybe there is one who fetches the image faster.
In my application, I use these 2 classes but I don't know which one I should prioritize.
Image.asset('icons/heart.png')
AssetImage('icons/hear.png')
Maybe there is one who fetches the image faster.
Image is a StatefulWidget and Image.asset is just a named constructor, you can use it directly on your widget tree.
AssetImage is an ImageProvider which is responsible for obtaining the image of the specified path.
If you check the source code of the Image.asset you will find that it's using AssetImage to get the image.
Image.asset(String name, {
Key key,
AssetBundle bundle,
this.semanticLabel,
this.excludeFromSemantics = false,
double scale,
this.width,
this.height,
this.color,
this.colorBlendMode,
this.fit,
this.alignment = Alignment.center,
this.repeat = ImageRepeat.noRepeat,
this.centerSlice,
this.matchTextDirection = false,
this.gaplessPlayback = false,
String package,
this.filterQuality = FilterQuality.low,
}) : image = scale != null
? ExactAssetImage(name, bundle: bundle, scale: scale, package: package)
: AssetImage(name, bundle: bundle, package: package),
assert(alignment != null),
assert(repeat != null),
assert(matchTextDirection != null),
super(key: key);
Thanks @diegoveloper
From flutter version 2.5, it is recommended to use the Image StatefulWidget with the const modifier which is impossible with Image.asset. However, you'll need to provide the image path as a parameter to the AssetImage object, where this object is the value of the named parameter 'image' of the Image StatefulWidget, like so.
Image(
image: AssetImage('asset/dice1.png'),
)
From the recommended Dart tutorial book Dart Apprentice const and final modifiers on objects reduce subsequent compile-time and runtime.
Therefore for clean and few lines of code, use Image.asset, but for fast and CPU-friendly code, use Image StatefulWidget.