Skip to content

Deserializing payment methods from the Stripe API  #81

Closed
@adouglass

Description

@adouglass

I'm making a call to my REST API to fetch a user's list of saved payment methods-- which returns the JSON provided via the Stripe API for a List.

  Future<List<PaymentMethod>> fetchPaymentMethods() async {
      final response = await http.get(
        uriPaymentMethods(),
        headers: {
          "Accept": "application/json",
          "content-type": "application/json",
          if (_sessionToken != null)
            HttpHeaders.authorizationHeader: 'Bearer $_sessionToken',
        },
      );
      final responseJson = HttpUtil.returnResponse(response) as List;
      return responseJson
          .map((json) => PaymentMethod.fromJson(json))
          .toList();
  }

The .fromJson method attempts to deserialize the payment method and errors on the Card property. It seems as though the generated .fromJson method is expecting Card with a capital C -- but the Stripe API returns all properties in lowercase.

flutter: NoSuchMethodError: The method '[]' was called on null.
Receiver: null
Tried calling:
flutter: #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
#1 $$_CardFromJson
package:stripe_platform_interface/…/models/payment_methods.g.dart:97
#2 new _$_Card.fromJson
package:stripe_platform_interface/…/models/payment_methods.freezed.dart:1513
#3 _$CardFromJson
package:stripe_platform_interface/…/models/payment_methods.freezed.dart:1323
#4 new Card.fromJson
package:stripe_platform_interface/…/models/payment_methods.dart:156
#5 $$_PaymentMethodFromJson
package:stripe_platform_interface/…/models/payment_methods.g.dart:16
#6 new _$_PaymentMethod.fromJson
package:stripe_platform_interface/…/models/payment_methods.freezed.dart:439
#7 _$PaymentMethodFromJson
package:stripe_platform_interface/…/models/payment_methods.freezed.dart:16
#8 new PaymentMethod.fromJson
package:stripe_platform_interface/…/models/payment_methods.dart:66

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions