Image.file
- Flutter course
- Beginner
- Lesson 78/99
- 4 min
Show on-device file images with Image.file.
What is Image.file?
Image.file is used to show an image from the device's file system. It takes a File object from the dart:io library; not an asset or URL. It is useful for images picked from the gallery or downloaded to the app.
import 'dart:io';
Image.file(File('/data/user/0/app/photo.png'))dart:io and file paths
When using Image.file you must import the File class from dart:io. The file path varies by platform; on Android it's typically under the app directory, on desktop it's a full path. If the file doesn't exist, the app throws an error.
import 'dart:io';
final file = File('/path/to/image.png');
Image.file(file)Practice Show a file image with Image.file
Import File from dart:io and show the image at /tmp/photo.png with Image.file.
Starter code
import 'package:flutter/material.dart';
// dart:io'dan File içe aktar
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
// Image.file ile /tmp/photo.png göster
child: null,
),
),
);
}
}Sample solution
import 'package:flutter/material.dart';
import 'dart:io';
void main() {
print('FileImage: /tmp/photo.png');
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Image.file(File('/tmp/photo.png')),
),
),
);
}
}Expected output
FileImage: /tmp/photo.png
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What parameter type does Image.file take?
- A string asset path
- A File object Correct answer
- A Uint8List byte array
- A string URL
Explanation: Image.file expects a dart:io File object; it does not take a URL or asset path string.
2 Pass a File object to Image.file
Image.file(____)
Image.file(File('/path/to/p.png'))
Explanation: Image.file takes a File object; not a string or AssetImage. The path must also point to a file.
3 Image.file parts
| Term | Meaning |
|---|---|
Image.file |
Loads an image from the file system |
dart:io |
Source of the File class |
File(...) |
The object wrapping the file path |
Dosya yolu |
Device-specific location |