我正在使用快取的網路影像,有時某些影像會拋出錯誤,當然包會處理此錯誤,但是當我按下此圖示時我想再次重新加載此影像,我該怎么做
uj5u.com熱心網友回復:
我相信你只需要在GestureDetector你的錯誤小部件中添加一個并在那里處理重新加載
CachedNetworkImage(
imageUrl: "http://via.placeholder.com/350x150",
placeholder: (context, url) => CircularProgressIndicator(),
errorWidget: (context, url, error) => GestureDetector(
onTap: () {
// handle your reload and setState/ state management here
},
child: Icon(Icons.error) // the placeholder/error image,
),
);
編輯 1:使用 bloc 庫(cubit 是簡單的函式)
var attempts = 0;
return BlocBuilder<YourCubit, YourState>(
bloc: _yourCubitInstance,
builder: (context, state) {
return CachedNetworkImage(
imageUrl: "http://via.placeholder.com/350x150",
placeholder: (context, url) => CircularProgressIndicator(),
errorWidget: (context, url, error) => GestureDetector(
onTap: () {
//attempts is needed to ensure that retry state is emitted as
// bloc library prevents exact same states for being emitted
attempts
_yourCubitInstance.refreshImage(attempt: attempts)
// above method should emit state to rebuild this widget
// thus retrying the url load
},
child: Icon(Icons.error) // the placeholder/error image,
),
);
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/330688.html
