Flutter 图像资源服务捕获异常-Crashlytics非致命错误-http请求失败
我正在尝试加载产品列表,并且我正在使用CachedNetworkImage(或者如果我也使用Image.network)来显示该特定产品的图像 我使用了errorbuilder属性,所以若图像出现错误,那个么显示默认的资产图像,它就可以正常工作了。在我将另一个页面推到上面之前,我会在调试控制台中收到以下消息Flutter 图像资源服务捕获异常-Crashlytics非致命错误-http请求失败,flutter,flutter-dependencies,dart-pub,getx,flutter-getx,Flutter,Flutter Dependencies,Dart Pub,Getx,Flutter Getx,我正在尝试加载产品列表,并且我正在使用CachedNetworkImage(或者如果我也使用Image.network)来显示该特定产品的图像 我使用了errorbuilder属性,所以若图像出现错误,那个么显示默认的资产图像,它就可以正常工作了。在我将另一个页面推到上面之前,我会在调试控制台中收到以下消息 I/flutter (21932): ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════
I/flutter (21932): ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
I/flutter (21932): The following NetworkImageLoadException was thrown resolving an image codec:
I/flutter (21932): HTTP request failed, statusCode: 404, https://***.com/url.jpg
然而,我在某个地方读到,如果我们收到这种错误,那么就忽略它,因为CachedNetworkImage作者在他的最后一行文档中也说过同样的话。但问题是我也在使用Firebase Crashlytics来显示崩溃和错误(如果有)
因为这个异常,我得到了非致命的错误。所以,即使有一个用户正在使用我的应用程序,他们也不会在屏幕上看到任何错误,但如果我使用crashlytics,我会得到超过48个特定用户的非致命错误。
这是我用来显示产品列表及其图像的代码
ProductTile.dart
import 'package:applicationName/views/product_page/model/ProductModel.dart';
import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:get/get.dart';
class ProductTile extends StatelessWidget {
final Product product;
const ProductTile(this.product);
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {
Get.toNamed("/productDetailPage/${product.prodId}");
},
child: Card(
elevation: 2,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
bottomRight: Radius.circular(8.0)),
color: Colors.indigo,
),
padding: EdgeInsets.all(4.0),
child: Text(
"${product.discount}% OFF",
textScaleFactor: 0.8,
style: TextStyle(
color: Colors.white,
),
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
),
SizedBox(width: 10),
],
),
Container(
// color: Colors.red,
child: Image.network(
product.image,
errorBuilder: (BuildContext context, Object exception,
StackTrace stackTrace) {
// Appropriate logging or analytics, e.g.
// myAnalytics.recordError(
// 'An error occurred loading "https://example.does.not.exist/image.jpg"',
// exception,
// stackTrace,
// );
return Image.asset('assets/images/noimage.png');
},
)
//I commented this to check if same is happening with Image.network() also or not and it's happening in //it too.
// CachedNetworkImage(
// alignment: Alignment.center,
// imageUrl: product.image,
// placeholder: (context, url) =>
// Center(child: CircularProgressIndicator()),
// errorWidget: (context, url, error) =>
// Image.asset('assets/images/noimage.png'),
// ),
),
SizedBox(height: 8),
Padding(
padding: const EdgeInsets.only(left: 8.0),
child: Text(
product.prodName,
maxLines: 2,
style: TextStyle(
fontFamily: 'avenir', fontWeight: FontWeight.w800),
overflow: TextOverflow.ellipsis,
),
),
SizedBox(height: 8),
Padding(
padding: const EdgeInsets.only(left: 8.0),
child: Flex(
direction: Axis.vertical,
children: [
Text(
// "Some Text",
// '₹${double.parse(product.fprice).toStringAsFixed(2)}',
double.tryParse(product.priceFormat) is double
? "₹${double.tryParse(product.priceFormat).toStringAsFixed(2)}"
: "${product.priceFormat}",
style: TextStyle(
// fontSize: 20,
fontFamily: 'avenir',
decoration: TextDecoration.lineThrough),
),
],
),
),
SizedBox(height: 8),
Padding(
padding: const EdgeInsets.only(left: 8.0, bottom: 8.0),
child: Flex(
direction: Axis.vertical,
children: [
Text(
// "Some Text",
//
// product.price is String
// ? '₹${double.parse(product.price).toStringAsFixed(2)}'
// : '₹${product.price.toStringAsFixed(2)}',
double.tryParse(product.fpriceFormat) is double
? "₹${double.tryParse(product.fpriceFormat).toStringAsFixed(2)}"
: "${product.fpriceFormat}",
style: TextStyle(
// fontSize: 15,
fontFamily: 'avenir',
),
),
],
),
),
],
),
),
);
}
}
无论我在上面的页面之后推哪个页面,我仍然会在控制台中得到该异常。uri无效吗?@JohnJoe是的,uri无效,这是显而易见的,因为没有为所有产品上载图像,只有一些产品存在一些图像,这就是我使用错误生成器的原因,因此如果没有找到图像,则使用错误生成器资产形象。