简体   繁体   English

如何在 flutter 中加载后显示网络图像

[英]How to show network image after loading in flutter

I had a bottom navigation bar我有一个底部导航栏

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),
    ),
    ),
  ],
);

} }

In backgroundImage: i am addressing a NetworkImage => userAvatar but when app loads then the value of userAvatar is null because it take some time to fetch the image.在 backgroundImage 中:我正在处理 NetworkImage => userAvatar 但是当应用程序加载时 userAvatar 的值是 null 因为它需要一些时间来获取图像。 But instantly it throws me error that url.= null is not true.但它立即给我带来了错误 url.= null 不是真的。 How to show image after loading without any error.加载后如何显示图像而没有任何错误。

You can use cached_network_image for this您可以为此使用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),
),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM