0

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

}

now i am creating getter

List<Vd> get vdata {
   return _vdList;
 }

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.

this is my main.dart

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

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

  • We need to see where you've instantiated your `Provider` (usually a `ChangeNotifierProvider`, usually done in main.dart) and check if you've done it in the correct place with the correct child. These links might help you: https://stackoverflow.com/a/66486075/2301224 and https://stackoverflow.com/a/66486075/2301224. – Baker Apr 28 '21 at 04:39
  • i edited question. but my problem is that when cousumer called then getter is return Empty array in getter there is no data how can i initialize data in getter. – Sachin Kumar Rajput Apr 28 '21 at 04:44
  • As a test, can you retrieve hardcoded data from your `CartProductNotifier` from within your `Consumer`? – Baker Apr 28 '21 at 04:47
  • actually i didn't try hardcoded with list type object.. but i am sure hardcoded data will come. i tried String, int type and it will come – Sachin Kumar Rajput Apr 28 '21 at 04:53
  • problem is that i am unable to pass data which will come from API to getter. – Sachin Kumar Rajput Apr 28 '21 at 04:54
  • and you've confirmed data is assigned here: `_vdList = value.data.product.vd;`? – Baker Apr 28 '21 at 04:55
  • yes i confirmed data is assigned here i checked by debugger. – Sachin Kumar Rajput Apr 28 '21 at 04:57
  • Did you forget to include the brackets for the `CartProductNotifier` class since it looks like you have just defined a class and put nothing in it? Or did you just type it wrong here? – Gabber235 Apr 28 '21 at 12:32
  • no no brackets are there in class i just typed here only for demo. edited. – Sachin Kumar Rajput Apr 28 '21 at 12:56

2 Answers2

0

Your ChangeNotifierProvider looks OK. You could specify the Type explicitly as ChangeNotifierProvider<CartProductListNotifier>, but if your Consumer widget isn't throwing an error, then it doesn't seem to be your problem.

I noticed you aren't notifying your consumers of an update in value when the async call completes. Adding a notifyListeners() would correct that:

callApi(String handle) {
    return _webServices.getProductDetails(handle).then((value) {
      _getProductDetailsModel = value;
      _vdList = value.data.product.vd;
      notifyListeners(); // ← if you want Consumer to rebuild when this call finishes
      return value;
    });
  }
Baker
  • 24,730
  • 11
  • 100
  • 106
0

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

example

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