What is Difference Between AssetImage and Image.asset In Flutter ?

· 2 min read
What is Difference Between AssetImage and Image.asset In Flutter
What is Difference Between AssetImage and Image.asset In Flutter

Earlier we have been through How to Compress Image Before Uploading to Firebase and Flutter So in this article we have been What is the Difference Between AssetImage and Image.asset In Flutter?

Are you ready for the same ??

What is the Difference Between AssetImage and Image.asset In Flutter?

Image is a Stateful Widget and Image.asset is just a named constructor, you can use it directly on your widget tree.

AssetImage is an ImageProvider that 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);

Example:

class ImageExample extends StatelessWidget {
  const ImageExample({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Asset Image"),
      ),
      body: Center(
        child: Image.asset('assets/1.jpg'),
      ),
    );
  }
}

Output

Conclusion:

Thanks for being with us on a Flutter Journey !!!

In this article, We have been through What is the Difference Between AssetImage and Image.assets in Flutter?

Keep Learning !!! Keep Fluttering !!!

FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

Leave a comment

Your email address will not be published. Required fields are marked *