Flutter 如何使用flatter处理url中的空图像错误
我有以下错误:Flutter 如何使用flatter处理url中的空图像错误,flutter,dart,Flutter,Dart,我有以下错误: ======== Exception caught by image resource service ================================================ The following ArgumentError was thrown resolving an image codec: Invalid argument(s): No host specified in URI file:///null When the excepti
======== Exception caught by image resource service ================================================
The following ArgumentError was thrown resolving an image codec:
Invalid argument(s): No host specified in URI file:///null
When the exception was thrown, this was the stack:
#0 _HttpClient._openUrl (dart:_http/http_impl.dart:2425:9)
#1 _HttpClient.getUrl (dart:_http/http_impl.dart:2346:48)
#2 NetworkImage._loadAsync (package:flutter/src/painting/_network_image_io.dart:89:59)
#3 NetworkImage.load (package:flutter/src/painting/_network_image_io.dart:50:14)
#4 ImageProvider.resolveStreamForKey.<anonymous closure> (package:flutter/src/painting/image_provider.dart:503:13)
...
Image provider: NetworkImage("null", scale: 1.0)
Image key: NetworkImage("null", scale: 1.0)
但我还是发现了同样的错误
以下是我的完整代码:
import 'package:flutter/material.dart';
Widget buildArticleItem(article) {
return Padding(
padding: const EdgeInsets.all(20.0),
child: Row(
children: [
Container(
width: 120,
height: 120,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(
10.0,
),
image: DecorationImage(
image: NetworkImage(
'${article['urlToImage']}'.isNotEmpty
? '${article['urlToImage']}'
: FlutterLogo(),
),
fit: BoxFit.cover,
),
),
),
SizedBox(
width: 20.0,
),
Expanded(
child: Container(
height: 120.0,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Expanded(
child: Text(
'${article['title']}',
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.w400,
),
maxLines: 4,
overflow: TextOverflow.ellipsis,
),
),
Text(
'${article['publishedAt']}',
style: TextStyle(
color: Colors.grey,
),
),
],
),
),
),
],
),
);
}
为了清楚起见,使用null调用图像url对象,如下所示:
当然还有其他非空的图像,它可以正常工作。您可以创建一个带有try-catch块的函数,该函数将返回一个图像小部件
Widget\u getImage(动态文章){
网络图像(NetworkImage);;
试一试{
_图像=网络图像(
“${article['urlToImage']}”。不空
?“${article['urlToImage']}”
:logo(),
);
}捕获(e){
debugPrint(e);//查看错误是什么
_image=NetworkImage(“某些默认URI”);
}
}
然后,简单地放入函数,而不是放置小部件
image: _getImage(article)
将逻辑移到网络映像之外
image: article['urlToImage'] != null
? DecorationImage(
image: NetworkImage(article['urlToImage']),
fit: BoxFit.cover,
)
: DecorationImage(
image: NetworkImage(
'urlImage'),
),
面对同样的问题,我认为这个问题可以通过放慢未来的脚步来解决,使用Future.delayed.现在就着手解决@xahid_rocks如果有任何更新,可以告诉我吗d您可以共享logo()代码吗?如果它是一个小部件,那么我认为将它传递给NetworkImage()会有问题,@AlirezaAbiri flatter logo是flatter中的内置小部件,这只是一个普通的小部件,我可以用普通的文本来更改它,但它给了我同样的效果error@MahmoudHarooney我尝试添加此代码,但发现参数类型“Widget”的编译错误无法分配给参数类型“ImageProvider”。@Mahmoudharoney您需要返回
\u image
并进行更改Widget\u getImage
toNetworkImage getImage
@SagarV我试图将代码更改为NetworkImage getImage(动态文章){NetworkImage\u image;try{u image=NetworkImage('${article['urlToImage']}。是否空?${article['urlToImage']}):flatterlogo(),)}catch(e){debugPrint(e);//要查看错误是什么_image=NetworkImage(“某些默认URI”);}返回_image;}
并像这样调用它image:getImage(article),
我发现了相同的错误:(我面临一个错误,类型'flatterLogo'不是类型'DecorationImage'的子类型。
似乎我必须将同一类型与DecorationImage
放在一起,非常感谢它被解决了。我可以进行简单的编辑吗?:当然可以编辑!很高兴它也有帮助:)
image: article['urlToImage'] != null
? DecorationImage(
image: NetworkImage(article['urlToImage']),
fit: BoxFit.cover,
)
: DecorationImage(
image: NetworkImage(
'urlImage'),
),