Click here to Skip to main content
15,563,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I'm trying to implement a slider that add a certain value to the initial value when checking out. But I'm not able to pass that extra value to my checkout API Call, that is in the initSate. Any idea how to update the value inside initState?

Here an example: https://imgur.com/a/mp5ytzW

Dart
<pre>class DonateScreen extends StatefulWidget {
  final int itemId;
  final int conversationId;
  // ignore: prefer_typing_uninitialized_variables
  var extra;

  DonateScreen(@pathParam this.itemId, @pathParam this.conversationId, {Key? key})
      : super(key: key) {
    extra = 0;
  }

  @override
  _DonateScreenState createState() => _DonateScreenState();
}

class _DonateScreenState extends State<DonateScreen> {
  late Future<StripePaymentIntent> _stripePaymentIntent;

  int finalAmount = 0;

  @override
  void initState() {
    super.initState();
    setState(() {
      _stripePaymentIntent = Provider.of<PaymentIntentProvider>(context, listen: false)
          .fetchStripePaymentIntent(widget.itemId, widget.extra);
      
      print('old value: ${widget.extra}');
    });
  }

  @override
  Widget build(BuildContext context) {
    const sizedBoxSpace = SizedBox(height: 24);
    return BlocBuilder<ItemBloc, ItemState>(
      builder: (context, state) {
        if (state is ItemLoadedState) {
          Item item = state.item;
          return Scaffold(
            appBar: AppBar(
              title: Text(
                "Pay".tr(),
              ),
            ),
            body: SingleChildScrollView(
              child: BlocBuilder<ProfileBloc, ProfileState>(
                builder: ((context, state) {
                  if (state is ProfileLoadedState) {
                    UserProfile userProfile = state.userProfile;
                    return FutureBuilder<StripePaymentIntent>(
                        future: _stripePaymentIntent,
                        builder:
                            (BuildContext context, AsyncSnapshot<StripePaymentIntent> snapshot) {
                          List<Widget> children;
                          if (snapshot.hasData) {
                            Item theItem = item;
                            children = <Widget>[
                              BlocBuilder<NgosBloc, NgosState>(
                                builder: (context, state) {
                                  if (state is NgosLoadedState) {
                                    return Column(
                                      children: [
                                        Padding(
                                            padding: const EdgeInsets.symmetric(
                                                horizontal: 15, vertical: 15),
                                            child: Column(
                                              crossAxisAlignment: CrossAxisAlignment.start,
                                              children: [
                                                sizedBoxSpace,
                                                UserProfileWidget(userProfile),
                                                sizedBoxSpace,
                                                Text(
                                                  "donate1".tr(args: [userProfile.firstname]),
                                                  style: MyTheme.black,
                                                ),
                                                Text("donate2".tr(), style: MyTheme.black),
                                                const SizedBox(height: 10),
                                                Text("donate3".tr(), style: MyTheme.orange600),
                                                sizedBoxSpace,
                                                RichText(
                                                  text: TextSpan(
                                                    style: MyTheme.mediumBlack,
                                                    children: <TextSpan>[
                                                      TextSpan(text: 'donate4'.tr()),
                                                      TextSpan(
                                                          text: Utils.getDisplayPrice(finalAmount),
                                                          style: const TextStyle(
                                                              color: MyTheme.orange)),
                                                      TextSpan(text: 'donate5'.tr()),
                                                      TextSpan(
                                                          text: state.ngos[theItem.ngoId]!.name,
                                                          style: const TextStyle(
                                                              color: MyTheme.primary)),
                                                      TextSpan(text: 'donate6'.tr()),
                                                    ],
                                                  ),
                                                ),
                                                Slider(
                                                  min: 0,
                                                  max: 2000,
                                                  label: widget.extra.toString(),
                                                  value: widget.extra.toDouble(),
                                                  onChanged: ((value) {
                                                    setState(() {
                                                      widget.extra = value.toInt();
                                                      print(widget.extra);
                                                      finalAmount = theItem.amountInCent +
                                                          widget.extra as int;
                                                    });
                                                  }),
                                                ),
                                                PaymentsWidget(
                                                  snapshot.data!,
                                                  widget.conversationId,
                                                ),
                                              ],
                                            ))
                                      ],
                                    );
                                  }
                                  return Container();
                                },
                              ),
                            ];



What I have tried:

I tried this from https://stackoverflow.com/questions/55021127/pass-parameter-to-initstate but it's not working :(

Dart
@override
  void didUpdateWidget(DonateScreen oldWidget) {
    if (oldWidget.extra != widget.extra) {
      _stripePaymentIntent = Provider.of<PaymentIntentProvider>(context, listen: false)
          .fetchStripePaymentIntent(widget.itemId, widget.extra);
    }
    print('new value: ${widget.extra}');
    super.didUpdateWidget(oldWidget);
  }
Posted
Comments
Richard MacCutchan 7-Aug-22 7:50am    
"I tried this from https://stackoverflow.com/questions/55021127/pass-parameter-to-initstate but it's not working"
Then maybe you should ask the person at StackOverflow who wrote the suggested solution.
wseng 7-Aug-22 12:26pm    
how you pass extra to DonateScreen?
Digiyang 8-Aug-22 3:05am    
Well, I'm trying to pass extra directly in the DonateScreen.
To access the DonateScreen, I have to press a button in a conversation, but I don't want to pass the extra value as a parameter.
For me it doesn't make much sense, as the extra price has to be chosen on the DonateScreen.

Here an example: https://imgur.com/IRsjRMB

_handlePressDonate(Item? item) {
    if (item != null) {
      BlocProvider.of<ItemBloc>(context).add(LoadItem(item.id));
      BlocProvider.of<ProfileBloc>(context).add(LoadProfile(item.userId));
      context.router.push(
        DonateRoute(
            itemId: item.id, conversationId: widget.conversationId),
      );
    }
  }
Digiyang 8-Aug-22 4:23am    
I solved my problem actually :D
I did a bloc for my StripePayment with two separated events and I will post the answer later.
wseng 8-Aug-22 11:43am    
ok

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900