Null check operator used on a null value (flutter_stripe)

42 Views Asked by At

The Error says that I am getting a null somewhere. But I can't find where the value is null. I have integrated Flutter stripe to this project everthing was working Ok But don't know what happened. I have check the calling of the payment method is Ok no errors there. The issue is somewhere in this code.

import 'dart:convert';
import 'package:esim/src/mainpage/controller/main_controllers.dart';
import 'package:esim/src/mainpage/models/esim_model.dart';
import 'package:esim/src/qrscreens/controllers/activation_controller.dart';
import 'package:esim/src/qrscreens/views/qr_screen.dart';
import 'package:flutter/material.dart';
import 'package:flutter_stripe/flutter_stripe.dart';
import 'package:http/http.dart' as http;

class StripePaymentHandle {
  Future<void> payment({
    required BuildContext context,
    required String amount,
    required String regionName,
    required String planUid,
  }) async {
    try {
      print(amount);
      Map<String, String> body = {
        'amount': amount,
        'currency': 'EUR',
        'payment_method_types[]': 'card',
        // ...cardDetails,
      };
      var response = await http.post(
        Uri.parse('https://api.stripe.com/v1/payment_intents'),
        headers: {
          'Authorization':
              'Bearer Bearer my-test-key',
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        body: body,
      );
      if (response.statusCode == 200) {
        Map<String, dynamic> paymentIntentData = json.decode(response.body);
        String clientSecret = paymentIntentData['client_secret'];
        await initializePaymentSheet(
            context, clientSecret, regionName, planUid, paymentIntentData);
      } else {
        throw Exception('Failed to create payment intent');
      }
    } catch (e) {
      print('Error creating payment intent: $e');
    }
  }

  Future<void> initializePaymentSheet(
    BuildContext context,
    String clientSecret,
    String regionName,
    String planUid,
    Map<String, dynamic> paymentIntentData,
  ) async {
    try {
      await Stripe.instance.initPaymentSheet(
        paymentSheetParameters: SetupPaymentSheetParameters(
          paymentIntentClientSecret: clientSecret,
          style: ThemeMode.light,
          merchantDisplayName: 'merchant',
          allowsDelayedPaymentMethods: true,
        ),
      );
      presentPaymentSheet(context, regionName, planUid, paymentIntentData);
    } catch (e) {
      print('Error initializing payment sheet: $e');
    }
  }

  Future<void> presentPaymentSheet(BuildContext context, String regionName,
      String planUid, Map<String, dynamic> paymentIntentData) async {
    try {
      await Stripe.instance.presentPaymentSheet();
      retrieveTxnId(
          context: context,
          paymentIntent: paymentIntentData['id'],
          planUid: planUid,
          regionName: regionName);
    } catch (e) {
      print('Error presenting payment sheet: $e');
    }
  }

  retrieveTxnId(
      {required BuildContext context,
      required String paymentIntent,
      required String planUid,
      required String regionName}) async {
    try {
      http.Response response = await http.get(
          Uri.parse(
              'https://api.stripe.com/v1/charges?payment_intent=$paymentIntent'),
          headers: {
            "Authorization":
                "Bearer my-test-key",
            "Content-Type": "application/x-www-form-urlencoded"
          });

      if (response.statusCode == 200) {
        EsimResponse esimResponse =
            await MainController().creatingEsim(regionName, planUid);
        ActivationController().userEsim(
          planiD: planUid,
          iCCID: esimResponse.esim.iccid,
        );
        Navigator.push(  //code breaks here
          context,
          MaterialPageRoute(
            builder: (context) => QrScreen(esim: esimResponse),
          ),
        );
      }
    } catch (e) {
      throw Exception(e.toString());
    }
  }
}
0

There are 0 best solutions below