Unverified Commit 363ea406 authored by Juanjo Tugores's avatar Juanjo Tugores Committed by GitHub

Improve the error message when calling `Image.file` on web (#94568)

parent ed1d075a
......@@ -464,7 +464,14 @@ class Image extends StatefulWidget {
this.filterQuality = FilterQuality.low,
int? cacheWidth,
int? cacheHeight,
}) : image = ResizeImage.resizeIfNeeded(cacheWidth, cacheHeight, FileImage(file, scale: scale)),
}) :
// FileImage is not supported on Flutter Web therefore neither this method.
assert(
!kIsWeb,
'Image.file is not supported on Flutter Web. '
'Consider using either Image.asset or Image.network instead.',
),
image = ResizeImage.resizeIfNeeded(cacheWidth, cacheHeight, FileImage(file, scale: scale)),
loadingBuilder = null,
assert(alignment != null),
assert(repeat != null),
......
......@@ -7,6 +7,7 @@
@Tags(<String>['reduced-test-set'])
import 'dart:async';
import 'dart:io';
import 'dart:math' as math;
import 'dart:typed_data';
import 'dart:ui' as ui;
......@@ -1956,6 +1957,22 @@ void main() {
matchesGoldenFile('image_test.missing.2.png'),
);
}, skip: kIsWeb); // https://github.com/flutter/flutter/issues/74935 (broken assets not being reported on web)
testWidgets('Image.file throws a non-implemented error on web', (WidgetTester tester) async {
const String expectedError =
'Image.file is not supported on Flutter Web. '
'Consider using either Image.asset or Image.network instead.';
final Uri uri = Uri.parse('/home/flutter/dash.png');
final File file = File.fromUri(uri);
expect(
() => Image.file(file),
kIsWeb
// Web does not support file access, expect AssertionError
? throwsA(predicate((AssertionError e) => e.message == expectedError))
// AOT supports file access, expect constructor to succeed
: isNot(throwsA(anything)),
);
});
}
@immutable
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment