Sub topic 6
Showing, saving, and uploading the result
This chapter assumes you know Dart async and await, Flutter widgets, and how to add packages to pubspec.yaml. It also assumes you have an XFile from image_picker or camera.
An XFile is a cross-platform file reference. It provides a path, a file name, and methods such as readAsBytes() and saveTo(). Treat the reference as temporary until you copy it to storage or upload it.
Showing an XFile
On mobile and desktop, Image.file reads a path from the local file system. On the web, the path is usually a browser blob URL, so use Image.network instead.
import 'dart:io' show File;
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
Widget imagePreview(XFile file) {
if (kIsWeb) {
return Image.network(file.path, fit: BoxFit.cover);
}
return Image.file(
File(file.path),
fit: BoxFit.cover,
);
}The dart:io import must not be used by web-only code. The conditional kIsWeb prevents the widget from choosing Image.file in a browser, but importing dart:io can still affect web compilation in some project structures. For a production app that targets every platform, isolate mobile file-system code behind platform-specific files or use a package that provides a cross-platform image widget.
Saving a picked file
A picker may return a temporary file. Android can remove temporary files when it needs storage, so copy a file that your app must keep.
Add path_provider to obtain an application directory:
dependencies:
path_provider: ^2.1.5Then save the file:
import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';
Future<String> savePermanently(XFile source) async {
final directory = await getApplicationDocumentsDirectory();
final destination =
'${directory.path}/${source.name}';
await source.saveTo(destination);
return destination;
}saveTo copies the contents to the destination. If users need to find the file in their device gallery, an app documents directory is usually not enough. Use a media-storage solution designed for that platform.
Uploading the bytes
A cross-platform upload should read the XFile as bytes instead of assuming that its path is readable by dart:io. The http package can create a multipart request from those bytes.
import 'package:http/http.dart' as http;
import 'package:image_picker/image_picker.dart';
Future<void> uploadImage(XFile image) async {
final request = http.MultipartRequest(
'POST',
Uri.parse('https://example.com/api/images'),
);
request.files.add(
http.MultipartFile.fromBytes(
'image',
await image.readAsBytes(),
filename: image.name,
),
);
final response = await request.send();
if (response.statusCode < 200 || response.statusCode >= 300) {
throw Exception('Upload failed: ${response.statusCode}');
}
}For large videos, reading the entire file into memory may be expensive. Prefer a streaming or platform-specific upload approach when the API and package support it. Always show progress and provide retry behavior for real uploads.
Exercise
Exercise
Why should an upload use readAsBytes instead of assuming that XFile.path is a local file-system path?
Show answer
The path may be a browser blob URL on the web, while readAsBytes works through XFile's cross-platform interface.