Images make apps look nice and help users understand content better. The Image widget shows pictures from files or the internet.
Image widget (asset, network) in Flutter
Image.asset('path/to/image.png') Image.network('https://example.com/image.png')
Image.asset loads images bundled inside your app.
Image.network loads images from the internet using a URL.
Image.asset('assets/logo.png')Image.network('https://flutter.dev/images/flutter-logo-sharing.png')Image.asset('assets/photo.jpg', width: 100, height: 100)
Image.network('https://example.com/pic.jpg', fit: BoxFit.cover)This app shows two images stacked vertically: one local asset image and one from the internet. Both images are 150 pixels wide and centered on the screen.
import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('Image Widget Example')), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Image.asset('assets/flutter.png', width: 150), const SizedBox(height: 20), Image.network('https://flutter.dev/images/flutter-logo-sharing.png', width: 150), ], ), ), ), ); } }
Make sure to add local images to your pubspec.yaml under assets: to use Image.asset.
Network images need internet access and may take time to load; consider showing a placeholder or loading spinner.
You can control image size with width and height properties.
The Image widget displays pictures in Flutter apps.
Use Image.asset for local files bundled with your app.
Use Image.network to load images from the internet.
Practice
Solution
Step 1: Understand image sources in Flutter
Flutter provides different constructors for Image widget depending on the source: asset, network, file, or memory.Step 2: Identify the correct constructor for local assets
Images bundled with the app are accessed usingImage.asset.Final Answer:
Image.asset -> Option AQuick Check:
Local image = Image.asset [OK]
- Using Image.network for local files
- Confusing Image.file with assets
- Not adding assets to pubspec.yaml
Solution
Step 1: Identify the correct constructor for network images
To load images from the internet, Flutter usesImage.networkwith the URL as a string parameter.Step 2: Check syntax correctness
The syntaxImage.network('https://example.com/image.png')is correct and will load the image from the URL.Final Answer:
Image.network('https://example.com/image.png') -> Option BQuick Check:
Network image = Image.network [OK]
- Using Image.asset for URLs
- Passing URL to Image.file
- Missing quotes around URL string
Image.asset('images/logo.png')Assuming the image file exists and is declared in pubspec.yaml.
Solution
Step 1: Confirm asset image usage
The code usesImage.assetwith a relative path 'images/logo.png', which means it loads a local asset image.Step 2: Check conditions for display
Since the image exists and is declared in pubspec.yaml, Flutter will display the image correctly.Final Answer:
The image 'logo.png' from the assets folder is shown -> Option CQuick Check:
Image.asset with valid asset = image shown [OK]
- Confusing asset path with network URL
- Not declaring asset in pubspec.yaml
- Expecting loading spinner for local images
Image.asset('https://example.com/image.png')Solution
Step 1: Understand Image.asset usage
Image.assetis for local asset files, not URLs.Step 2: Identify misuse of URL with Image.asset
Passing a network URL string toImage.assetcauses a runtime error because it expects a local asset path.Final Answer:
Using Image.asset with a network URL causes an error -> Option AQuick Check:
Image.asset + URL = error [OK]
- Thinking Image.asset works with URLs
- Ignoring asset declaration in pubspec.yaml
- Confusing Image.file with Image.asset
Solution
Step 1: Understand fallback handling in Image.asset
Flutter'sImage.assetsupports anerrorBuildercallback to handle loading errors and show alternative widgets.Step 2: Check which option uses errorBuilder correctly
Image.asset('assets/icons/icon.png', errorBuilder: (context, error, stackTrace) => Image.network('https://example.com/fallback.png')) useserrorBuilderto show a network image if the asset fails, which is the correct approach.Final Answer:
Image.asset with errorBuilder showing Image.network fallback -> Option DQuick Check:
Use errorBuilder for fallback images [OK]
- Using fallback or onError props that don't exist
- Passing URLs to Image.asset directly
- Confusing Image.file with asset images
Ready to prove it?
Pass the test and this pattern moves unknown
