我正在尝试构建一个flutter
桌面[Windows]应用程序(我看着this link,当然我也克隆了这个repo:
在我的应用中,我想显示存储在本地磁盘中的图像库,因此在查看文档时,我正在使用Image.file。
现在,如果文件夹中有任何无效图像的文件,我将无法捕获任何可能发生的错误。
我试图在try-catch块中调用decodeImage
函数(来自package:image/image.dart
),但是某些文件的检查失败(在我的示例中,.txt文件将输入捕获块,而“损坏”的.jpg不会进入捕获块)。
对于"broken" .jpg
文件,出现此运行时错误:
C:\flutter\flutter-desktop-embedding\example>flutter run
Launching lib\main.dart on Windows in debug mode...
Building Windows application...
flutter: Working decode
flutter: Working decode
flutter: Error!
flutter: ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
flutter: The following _Exception was thrown resolving an image codec:
flutter: Exception: Could not instantiate image codec.
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #0 _futurize (dart:ui/painting.dart:4304:5)
flutter: #1 instantiateImageCodec (dart:ui/painting.dart:1682:10)
flutter: #2 PaintingBinding.instantiateImageCodec (package:flutter/src/painting/binding.dart:88:12)
flutter: #3 FileImage._loadAsync (package:flutter/src/painting/image_provider.dart:653:24)
flutter: <asynchronous suspension>
flutter: #4 FileImage.load (package:flutter/src/painting/image_provider.dart:638:14)
flutter: #5 ImageProvider.resolve.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:327:17)
flutter: #6 ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:160:22)
flutter: #7 ImageProvider.resolve.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:325:84)
flutter: #8 SynchronousFuture.then (package:flutter/src/foundation/synchronous_future.dart:38:29)
flutter: #9 ImageProvider.resolve.<anonymous closure> (package:flutter/src/painting/image_provider.dart:323:11)
flutter: #13 ImageProvider.resolve (package:flutter/src/painting/image_provider.dart:315:16)
flutter: #14 _ImageState._resolveImage (package:flutter/src/widgets/image.dart:1010:20)
flutter: #15 _ImageState.didChangeDependencies (package:flutter/src/widgets/image.dart:967:5)
flutter: #16 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4376:12)
flutter: #17 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
flutter: #18 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #19 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #20 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
flutter: #21 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
flutter: #22 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
flutter: #23 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
flutter: #24 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #25 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5551:32)
flutter: #26 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #27 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #28 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
flutter: #29 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #30 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #31 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
flutter: #32 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #33 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #34 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
flutter: #35 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #36 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #37 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
flutter: #38 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #39 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #40 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
flutter: #41 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
flutter: #42 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
flutter: #43 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
flutter: #44 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #45 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #46 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
flutter: #47 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #48 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #49 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
flutter: #50 Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
flutter: #51 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4206:5)
flutter: #52 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4381:11)
flutter: #53 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
flutter: #54 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #55 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #56 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
flutter: #57 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
flutter: #58 Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
flutter: #59 ComponentElement.performRebuild
flutter: (elided 11 frames from package dart:async and package dart:async-patch)
[...................]
flutter:
flutter: Path: C:/flutter/test_broken_image.jpg
flutter: ════════════════════════════════════════════════════════════════════════════════════════════════════
Syncing files to device Windows... 10.675ms (!)
? To hot reload changes while running, press "r". To hot restart (and rebuild state), press "R".
An Observatory debugger and profiler on Windows is available at: http://127.0.0.1:57907/Fes2Y_5FvgM=/
For a more detailed help message, press "h". To detach, press "d"; to quit, press "q".
在构建窗口小部件之前如何捕捉此错误,以便可以使用(例如)默认图标来处理它?</ p>
这是我的main.dart
源代码:
import 'dart:io';
import 'package:flutter/foundation.dart'
show debugDefaultTargetPlatformOverride;
import 'package:flutter/material.dart';
import 'package:image/image.dart' hide Image;
void main() {
// See https://github.com/flutter/flutter/wiki/Desktop-shells#target-platform-override
debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia;
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
List<Widget> buildImageList() {
final widgets = new List<Widget>();
final fileNameList = [
'C:/flutter/elephant.jpg',
'C:/flutter/test_broken_image.jpg',
'C:/flutter/txt_text.txt'
];
var fileList = fileNameList.map((fileName) => File(fileName)).toList();
fileList.forEach((f) {
try {
decodeImage(f.readAsBytesSync());
widgets.add(
new Container(
child: Image.file(f, width: 100),
),
);
print('Working decode');
} catch (err) {
print('Error!');
}
});
return widgets;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'title',
home: Scaffold(
appBar: AppBar(
title: Text('title'),
),
body: Scrollbar(
child: SingleChildScrollView(
child: Wrap(
children: buildImageList(),
),
),
),
),
);
}
}