本文介绍了无法在Changenotifier类中创建吸气剂.Flutter中使用提供程序时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是供应商的新手.我在这里写了我有问题的步骤.1-我在模型类中有数据之后调用了api.2-然后在ChangeNotifer类中,我要获取在Model类对象中的列表的吸气剂.我无法将模型类响应数据添加到此getter变量.

hello i am new with provider. i wrote steps here where i have problem.1- i called api after that i have data in my Model class.2- then in ChangeNotifer class i want to make getter of list which is in Model class object.i am unable to add model class response data to this getter variable.

class CartProductNotifier with ChangeNotifier{

final _webServices = WebServices();
GetProductDetailsModel _getProductDetailsModel;
List<Vd> _vdList = [];

callApi(String handle) {
    return _webServices.getProductDetails(handle).then((value) {
      _getProductDetailsModel = value;
      _vdList = value.data.product.vd; // after api call done data is in _vdList
      return value;
    });
  }

}

现在我正在创建吸气剂

List<Vd> get vdata {
   return _vdList;
 }

但是当我从我的 ProductDetailsS​​creen 类中调用此吸气剂时,我得到的是空数组或空值.

but when i called this getter from my ProductDetailsScreen class i am getting blank array or null.

 Consumer<CartProductListNotifier>(
                  builder: (context, value, child) {
                    return Text(
                       value.vdata[index].quantitySelectedByUser.toString(),  //data is not coming here
                    style: TextStyle(
                    fontSize: kFontSize16, fontWeight: kMediumFontWeight, color: Colors.black),
                    );
                  },
                ),

现在我无法在消费者"通知程序中的文本"小部件中更新值.

now i am unable to update value in Text widget which is in Consumer notifier.

这是我的main.dart

this is my main.dart

MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (context) => CartProductListNotifier(),
        ),
      ],
      child: MaterialApp(
        title: 'Application',
        debugShowCheckedModeBanner: false,

请解决此问题,或告诉我另一种通过Provider与Model类一起调用Api的好方法.预先感谢.

please solve this issue or told me another good way to Call Api with Model class with Provider.Thanks in Advance.

推荐答案

调用API解决后,调用setData函数解决了我的问题,现在我在_vdList中得到结果.

call setData function after calling API is solved my problem now i am getting result in _vdList.

示例

setData(GetProductDetailsModel _productModel) {
   _getProductDetailsModel = _productModel;
   _vdList = _productModel.data.product.vd;
   _voList = _productModel.data.product.vo;
   notifyListeners();
 }

这篇关于无法在Changenotifier类中创建吸气剂.Flutter中使用提供程序时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 13:10