Flutter Image.network()在没有网络连接的情况下会导致AnimatedList崩溃。

huangapple go评论104阅读模式
英文:

Flutter Image.network() keeps crashing in AnimatedList if there is no Internet connection

问题

在我的Flutter应用中,我使用Image.network()小部件如下来显示一张图片:

  1. Image.network(
  2. url,
  3. errorBuilder: (context, error, stacktrace) => Container(color: Colors.red),
  4. width: 80,
  5. height: 80,
  6. cacheWidth: 80,
  7. cacheHeight: 80,
  8. )

该小部件是在一个AnimatedList中使用的一部分。

如果我在列表中向下滚动而没有互联网连接,有时我的应用程序会崩溃,告诉我无法访问url,尽管我使用了errorBuilder:

  1. 以下SocketException在解析图像编解码时抛出:
  2. 主机查找失败:'i.discogs.com' (OS错误:无与主机名关联的地址,errno = 7)

所以我尝试了另一种方法,使用了cached_network_image包:

  1. CachedNetworkImage(
  2. placeholder: (context, url) => Container(color: Colors.red),
  3. errorWidget: (context, url, error) => Container(color: Colors.red),
  4. imageUrl: url,
  5. memCacheWidth: 80,
  6. memCacheHeight: 80,
  7. )

但是再次出现了类似的错误:

  1. 以下_ClientSocketException在解析图像编解码时抛出:
  2. 主机查找失败:'i.discogs.com'

我该如何修复这个问题?

编辑

  • 我创建了一个示例项目这里,您可以轻松地查看问题

  • 我报告了一个问题这里

  • 我还为cached_network_image报告了一个问题这里

英文:

In my Flutter app, I use a Image.network() widget as follows to display an image:

  1. Image.network(
  2. url,
  3. errorBuilder: (context, error, stacktrace) => Container(color: Colors.red),
  4. width: 80,
  5. height: 80,
  6. cacheWidth: 80,
  7. cacheHeight: 80,
  8. )

That widget is part of an item used in a AnimatedList.

If I'm scrolling down my list and I don't have an Internet connection, sometimes my app crashes telling me that it can't reach url, even though I use errorBuilder:

  1. The following SocketException was thrown resolving an image codec:
  2. Failed host lookup: 'i.discogs.com' (OS Error: No address associated with hostname, errno = 7)

So I tried another approach with the cached_network_image package:

  1. CachedNetworkImage(
  2. placeholder: (context, url) => Container(color: Colors.red),
  3. errorWidget: (context, url, error) => Container(color: Colors.red),
  4. imageUrl: url,
  5. memCacheWidth: 80,
  6. memCacheHeight: 80,
  7. )

But again, I get a similar error:

  1. The following _ClientSocketException was thrown resolving an image codec:
  2. Failed host lookup: 'i.discogs.com'

How can I fix that?

EDIT:

  • I created a sample project here, where you can easily see the problem

  • I reported a bug here.

  • I also reported a bug here for cached_network_image

答案1

得分: 1

好的,以下是您提供的内容的翻译:

原文:
Ok. I have tested your sample code.<br>
The root cause of the error is this code in your main function, which pushes a page on top of your page if error occurs.<br>

翻译:
好的,我已经测试了您的示例代码
错误的根本原因是您主函数中的这段代码,如果发生错误,它会在您的页面之上推送一个页面。

原文:
FlutterError.onError = (FlutterErrorDetails details) {
FlutterError.dumpErrorToConsole(details);
log(details.toString());
WidgetsBinding.instance.addPostFrameCallback((_) {
Navigator.push(
_navigatorKey.currentState!.context,
MaterialPageRoute(builder: (context) => ErrorPage(error: details.toString())),
);
});
};

Solution<br>

FlutterError.onError = (FlutterErrorDetails details) {
FlutterError.dumpErrorToConsole(details);
log(details.toString());
WidgetsBinding.instance.addPostFrameCallback((_) {
print("test is it called?");
// Don't push this widget to stack
// Navigator.push(
// _navigatorKey.currentState!.context,
// MaterialPageRoute(builder: (context) => ErrorPage(error: details.toString())),
// );
});
};
};

翻译:
解决方案:

FlutterError.onError = (FlutterErrorDetails details) {
FlutterError.dumpErrorToConsole(details);
log(details.toString());
WidgetsBinding.instance.addPostFrameCallback((_) {
print("测试是否被调用?");
// 不要将此小部件推送到堆栈
// Navigator.push(
// _navigatorKey.currentState!.context,
// MaterialPageRoute(builder: (context) => ErrorPage(error: details.toString())),
// );
});
};
};

英文:

Ok. I have tested your sample code.<br>
The root cause of the error is this code in your main function, which pushes a page on top of your page if error occurs.<br>

  1. FlutterError.onError = (FlutterErrorDetails details) {
  2. FlutterError.dumpErrorToConsole(details);
  3. log(details.toString());
  4. WidgetsBinding.instance.addPostFrameCallback((_) {
  5. Navigator.push(
  6. _navigatorKey.currentState!.context,
  7. MaterialPageRoute(builder: (context) =&gt; ErrorPage(error: details.toString())),
  8. );
  9. });
  10. };

Solution<br>

  1. FlutterError.onError = (FlutterErrorDetails details) {
  2. FlutterError.dumpErrorToConsole(details);
  3. log(details.toString());
  4. WidgetsBinding.instance.addPostFrameCallback((_) {
  5. print(&quot;test is it called?&quot;);
  6. // Don&#39;t push this widget to stack
  7. // Navigator.push(
  8. // _navigatorKey.currentState!.context,
  9. // MaterialPageRoute(builder: (context) =&gt; ErrorPage(error: details.toString())),
  10. // );
  11. });
  12. };
  13. };

huangapple
  • 本文由 发表于 2023年6月15日 16:31:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76480593.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定