getCustomerDetails method

Future<void> getCustomerDetails(
  1. String id
)

tries to fetch customer details using id, if not found, new customer will be created using id as an email address to fetch with it next time.

Implementation

Future<void> getCustomerDetails(String id) async {
  try {
    final customer = await checkout.getCustomerDetails(id);

    final Instrument? defaultInstrument =
        await checkout.getDefaultInstrument(customer.instruments);

    if (mounted) {
      state = AsyncValue.data(
        customer.copyWith(defaultInstrument: defaultInstrument, name: name),
      );
    }
  } catch (e) {
    //not found

    final customer = Customer(
      id: "",
      email: id,
      name: name,
      instruments: [],
    );

    final customerId = await checkout.createCustomer(customer);

    if (mounted) state = AsyncValue.data(customer.copyWith(id: customerId));
  }
}