Home > database >  How to solve provider string problem in Flutter?
How to solve provider string problem in Flutter?

Time:05-08

Hello I'am having a hard time changing the input value of a widget https://pub.dev/packages/stepper_counter_swipe.
I need to init the parameter initialValue to the quantity behind stockQuantity.
I got this error :

The following _CastError was thrown building _InheritedProviderScope<ProductManagmentModel?>(
    value: Instance of 'ProductManagmentModel', 
    listening to value):
type 'String' is not a subtype of type 'int' in type cast

With the following code

Selector<ProductManagmentModel, bool>(
builder: (_, manageStock, __) {
return Column(
    children: [
    if (manageStock)

        StepperSwipe(
        initialValue:stockQuantity1,
        speedTransitionLimitCount: 3,
        firstIncrementDuration: Duration(milliseconds: 300),
        secondIncrementDuration: Duration(milliseconds: 100),
        direction: Axis.horizontal,
        dragButtonColor: Colors.blueAccent,
        withSpring: true,
        maxValue:1000,
        minValue:0,
        withFastCount: true,
        stepperValue:widget.val,
        onChanged: (val) {
                model.product?.stockQuantity = int.parse(val.toString());
            },
        ),

    Row(
        children: [
        Checkbox(
            value: manageStock,
            onChanged: (val) {
                model.updateManageStock();
            }),
        Text(S.of(context).manageStock),
        ],
    ),
    ],
);
},
selector: (_, provider) => provider.product!.manageStock),

This is how the stockQuantity1 value is set up

void _initController() {
    final product =
        Provider.of<ProductManagmentModel>(context, listen: false).product;
    productNameController.text = product?.name ?? '';
    regularPriceController.text = product?.regularPrice ?? '';
    salePriceController.text = product?.salePrice ?? '';
    sKUController.text = product?.sku ?? '';
    stockQuantity.text = product?.stockQuantity.toString() ?? '';
    shortDescription.text = product?.shortDescription ?? '';
    description.text = product?.description ?? '';
    tagsController.text =
        product?.tags.map((e) => e.name).toList().join(',') ?? '';
    stockQuantity1 = (product?.stockQuantity.toString()?? '') as int;
    //stockQuantity1 = product?.stockQuantity as QuantityInputType;
  }

When the checkbox is not checked, the value is null and i get this error. When the checkbox is checked, I don't have that problem.

Thank you for helping.

CodePudding user response:

Perhaps explain your use case a bit more.

You are saying "When the checkbox is not check the value is null and i got this error when the checkbox is checked i dont have that problem"

So the problem actually happens when product?.stockQuantity is null?

What should the initialValue be when product?.stockQuantity is null?

From your code it seems that when product?.stockQuantity is null, stockQuantity1 becomes an empty string (''). Perhaps change that part such that it is 0:

stockQuantity1 = (product?.stockQuantity.toString()?? 0) as int;

CodePudding user response:

I solved my problem with the following

if(product?.stockQuantity.toString() == 'null' ||product?.stockQuantity.toString() == null || stockQuantity.text == null || stockQuantity.text == 'null'   || stockQuantity == 'null'  || stockQuantity == null) {
  //stockQuantity1=0 as int;
  stockQuantity1 = (product?.stockQuantity ?? 0);
}
else  {
  stockQuantity1 = (product?.stockQuantity ?? '') as int;
}
  • Related