flutter - 颤振异常生成器不断被调用
问题描述
我正在使用颤动的 Image.file() 在我的应用程序中显示图像。我还使用 errorBuilder 来处理任何崩溃并向用户显示消息。
执行这些步骤时遇到问题。
- 加载一个好的图像
- 将损坏的图像加载到同一个 Image.File() 小部件中
- 将原始好的图像加载回相同的 Image.File() 小部件
在传入损坏的照片(步骤 2)后,似乎每次文件更改都会导致显示错误生成器,而不是新的好图像。
如果我没有在第 2 步中传入损坏的照片,则照片会发生应有的变化。这是颤振 Image() 的错误,还是在它进入 errorBuilder 之后我应该做些什么。
这是我目前的设置。
Image.file(
file,
fit: BoxFit.cover,
errorBuilder: (BuildContext context, Object exception, StackTrace stackTrace) {
print("Failed to initialise the file");
print(stackTrace);
// Once an error occurs it always goes in here
return Text("an error occurred");
},
);
传入损坏文件时/之后,我收到的所有文件更改的实际错误是
Could not instantiate image codec.
更新
我写了一个 dartpad 来显示我遇到的问题。
https://dartpad.dev/98c2dacb481c088dfd2e5bee490f45ed
如果你点击
- 好形象
- 好形象 2
- 好形象
- 好形象 2
图像循环正确...有效。
如果您然后单击将尝试加载损坏的 jpeg 文件的“损坏的图像”,则会触发错误生成器。
如果您然后单击“良好图像”或“良好图像 2”,它们将不再构建,并且图像每次都被卡住加载错误生成器......我怎样才能让它再次加载其中一个好图像?
如果我不清楚,请告诉我,我会添加更多信息:)
非常感谢
解决方案
您可以在下面复制粘贴运行完整代码
这个错误可以用 add 修复key: UniqueKey()
,
我已经用 DardPad 测试它工作正常
代码片段
Image.network(
_selectedImageURL,
key: UniqueKey(),
工作演示
完整代码
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _goodImageOne =
"https://upload.wikimedia.org/wikipedia/commons/6/69/June_odd-eyed-cat_cropped.jpg";
String _corruptImage =
"https://srv-file16.gofile.io/download/hwTzLI/cat_corrupt.jpg";
String _goodImageTwo =
"https://upload.wikimedia.org/wikipedia/commons/c/c7/Tabby_cat_with_blue_eyes-3336579.jpg";
String _selectedImageURL;
@override
void initState() {
super.initState();
_selectedImageURL = _goodImageOne;
}
void _changeFile(String newUrl) {
setState(() {
_selectedImageURL = newUrl;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.network(
_selectedImageURL,
key: UniqueKey(),
height: 300,
width: 200,
loadingBuilder: (BuildContext context, Widget child,
ImageChunkEvent loadingProgress) {
if (loadingProgress == null) return child;
return Center(
child: CircularProgressIndicator(
value: loadingProgress.expectedTotalBytes != null
? loadingProgress.cumulativeBytesLoaded /
loadingProgress.expectedTotalBytes
: null,
));
},
errorBuilder: (BuildContext context, Object exception,
StackTrace stackTrace) {
return Text("Cannot display url");
},
),
Expanded(child: Container()),
RaisedButton(
onPressed: () => _changeFile(_goodImageOne),
child: Text("Good Image"),
),
RaisedButton(
onPressed: () => _changeFile(_corruptImage),
child: Text("Corrupt Image"),
),
RaisedButton(
onPressed: () => _changeFile(_goodImageTwo),
child: Text("Good Image 2"),
),
],
),
),
);
}
}
推荐阅读
- html - 图像、字体、网站图标等仅出现在 Safari 中,但不在 Chrome 或移动设备中?
- visual-studio-code - VSCode:将光标移动到每个光标的给定单词的下一个出现
- php - 更新关系内的关系 hasMany 值
- django - 重写 Django 模型以删除不必要的主键后收到 InvalidCursorName 错误消息
- javascript - 填写所有字段时显示 div
- sql-server - 我有以下代码可以使用 Python 从 SQL Server 编写输出:输出为 '1'、'2'、'3',但我想要 1 2 3
- windows - 如果 Win 键是使用 AutoHotkey 按下的唯一键,如何禁用它?
- python - Python - discord.py,如何修复“事件循环已关闭”
- python - 无法从 chatterbot 中的特定 mongo db 读取问题总是写入 chatterbot_database
- c++ - 如何检查 tensorflow 是否可以在我的系统上运行,例如是否找到了所有支持的硬件,如 avr 设备