Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type conversion fails for List<CustomModels> with nullable parameters #60

Open
DamienLeon opened this issue Apr 13, 2023 · 1 comment
Open

Comments

@DamienLeon
Copy link

Hi,
I have an AddressModel class with fromJson expecting a type of Map<String, dynamic> as few parameters are nullable. I store List<AddressModel> in another model and I have the TypeConverter for AddressModel as follows:

class AddressConverter extends TypeConverter<Address> {
  const AddressConverter() : super('jsonb');

  @override
  Map<String, dynamic> encode(Address value) => value.toJson();

  @override
  Address decode(dynamic value) {
    if (value is Map<String, dynamic>) {
      return Address.fromJson(value);
    } else {
      log(value.toString(), name: "Address Parsing Error");
      throw Exception("Parsing error: Unable to parse");
    }
  }
}

Now, the issue is that when the type conversion happens, it expects a List<Object> but finds a List<dynamic>. The workaround is to manually change List<Object> to List<dynamic> in the PostgresBinaryEncoder().convert() function from the postgres package as follows:

case PgDataType.jsonbArray:
        {
          if (input is List<dynamic>)  // Here, List<Object> is changed to List<dynamic>
          {
            final objectsArray = input.map((v) => utf8.encode(json.encode(v)));
            return writeListBytes<List<int>>(objectsArray, 3802, (item) => item.length + 1,
                (writer, item) {
              writer.writeUint8(1);
              writer.write(item);
            });
          }
          throw FormatException(
              'Invalid type for parameter value. Expected: List<Object> Got: ${input.runtimeType}');
        }

The code runs fine after his change and the data entry is added to the database. I am not sure if any changes can be made in AddressConverter code to resolve this issue.

Flutter 3.7.9 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 62bd79521d (13 days ago) • 2023-03-30 10:59:36 -0700
Engine • revision ec975089ac
Tools • Dart 2.19.6 • DevTools 2.20.1

stormberry: ^0.13.0
@schultek
Copy link
Owner

schultek commented Apr 13, 2023

Sounds like an issue with the postgres package. At least I don't have another idea right now so maybe just open a PR there to apply the fix with List<dynamic>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants