我想从NetworkImage小部件中调用提供者类的一个函数



我想在NetworkImage小部件中显示从我请求的端点返回的图像。要返回的每个图像都有一个单独的id。当向端点发出请求时,我需要更新此id。我该怎么做呢?

//provider
class InfoDetailsProvider with ChangeNotifier {
HttpService _httpService = HttpService();
Future<StorageItem> getImage(String imageId ) async {
StorageItem storageItem;
_httpService.initWithToken(authToken);
final urlImage= "http://example../$imageId";
try {
final result = await _httpService.request(url: urlImage, method: Method.GET);
if (result != null) {
if (result is d.Response) {
var responseData = result.data;     
storageItem=responseData.forEach((key,value){ });
notifyListeners();
return storageItem;
}
}
} on d.DioError catch (error) {
throw error;
}
return storageItem;
}
}

我想在信息细节屏幕上调用getImage函数来显示图像。是否可以在NetworkImage小部件中调用函数?

//Screen
class InfoDetailsState extends State<InfoDetailsScreen> {
var _isInit = true;
var _isLoading = false;
@override
void initState() {
super.initState();
}
@override
void didChangeDependencies() {
if (_isInit) {
setState(() {
_isLoading = true;
});   
Provider.of<InfoDetailsProvider>(context).getImage(imageId);
}
_isInit = false;
super.didChangeDependencies();
}
...
return Scaffold(
appBar: AppBar(),
body: RefreshIndicator(
....
child: SlidingUpPanel(
...
body: SafeArea(
child: Column(
children: <Widget>[
Container(                        
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
**  //Should I call function here?**
),
fit: BoxFit.fill,
),
));

就我对你问题的理解,解决办法是

InkWell(
onTap: callFunction(),
child: Container(
child: NetworkImage(),
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
**  //Should I call function here?**
),
fit: BoxFit.fill,
),
),
),
)

,你可以在build方法中定义调用provider函数的函数,比如

callFunction() {
Provider.of<CarsProvider>(context, listen: false).yourFunction();
}

如果你有更多的问题,请在评论中提问。:)

最新更新