我想从网络图窗口内调用提供商类的函数

发布于 2025-01-23 08:38:51 字数 2060 浏览 0 评论 0原文

我想在网络窗口小部件中显示从我要求的端点返回的图像。每个要返回的图像都有一个单独的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 Widget中调用函数?

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

I want to show the image returned from the endpoint that I requested, in the NetworkImage widget. Each image to return has a separate id. I need to update this id when making request to endpoint. How can I do that? I use provider class.

//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;
  }
}

I want to call getImage function in Info Details Screen to display image. Is it possible to call function with in NetworkImage widget?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爺獨霸怡葒院 2025-01-30 08:38:51

据我了解您的问题,解决方案是

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

,您可以定义您的函数,该功能将在build> build方法中调用提供商的函数,例如

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

如果您还有更多问题,请在评论中询问。 :)

As far as I understand your question, the solution is

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

and you can define your function that will call provider's function inside the build method like

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

if you have further more question, ask in comment. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文