flutter - 如何在flutter中加载后显示网络图像
问题描述
我有一个底部导航栏
Widget bottomNav(int index, PageController pageController){
return CustomNavigationBar(
currentIndex: index,
bubbleCurve: Curves.bounceIn,
scaleCurve: Curves.decelerate,
selectedColor: constantColors.blueColor,
unSelectedColor: constantColors.darkColor,
strokeColor: constantColors.blueColor,
scaleFactor: 0.1,
iconSize: 30,
onTap: (val){
index = val;
pageController.jumpToPage(val);
notifyListeners();
},
backgroundColor: Colors.white,
items: [
CustomNavigationBarItem(icon: Icon(EvaIcons.home)),
CustomNavigationBarItem(icon: Icon(EvaIcons.messageCircle)),
CustomNavigationBarItem(icon: CircleAvatar(
radius: 35,
backgroundColor: Colors.transparent,
backgroundImage: NetworkImage(userAvatar),
),
),
],
);
}
在 backgroundImage 中:我正在处理 NetworkImage => userAvatar 但是当应用程序加载时 userAvatar 的值为 null 因为它需要一些时间来获取图像。但它立即向我抛出了错误,即 url != null 不正确。加载后如何显示图像而没有任何错误。
解决方案
您可以为此使用cached_network_image
CachedNetworkImage(
imageUrl: "http://via.placeholder.com/200x150",
imageBuilder: (context, imageProvider) => Container(
decoration: BoxDecoration(
image: DecorationImage(
image: imageProvider,
fit: BoxFit.cover,
),
),
),
placeholder: (context, url) => CircularProgressIndicator(),
errorWidget: (context, url, error) => Icon(Icons.error),
),
推荐阅读
- python - 我不能在 python 3.x 中乘以数字
- sql - 是否可以将 SELECT ST_GeomFromText( 与其他 SELECT 一起使用?
- android - 如何确定 Google 地图何时在 Android 中说话
- websocket - 如何使用 ASP.NET Core SignalR 支持 100 万或更多 WebSocket 连接
- c# - 如何在文档查看器中包含的 xtrareport 中传递参数
- wordpress - SimplePie 错误 - Wordpress 上的 Curl 超时
- javascript - 尝试使用 VideoJS、VideoJS-YouTube、VideoJS-Playlist 和 VideoJS-Playlist-UI
- c# - 每个条目中具有不同维度的锯齿状整数数组
- r - 如何将 R Studio 连接到 Tableau?
- dc.js - dc.js HeatMap crossfilter 灰色未选中