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

Wrong from supabase generation for optional enum #520

Closed
TillJohanndeiter opened this issue Jan 1, 2025 · 3 comments
Closed

Wrong from supabase generation for optional enum #520

TillJohanndeiter opened this issue Jan 1, 2025 · 3 comments

Comments

@TillJohanndeiter
Copy link

TillJohanndeiter commented Jan 1, 2025

If a OfflineFirstWithSupabaseModel contains a optional enum then the generated code triggers a null pointer.

I have this class.

@ConnectOfflineFirstWithSupabase(
  supabaseConfig: SupabaseSerializable(tableName: 'training_record'),
)
class TrainingRecord extends OfflineFirstWithSupabaseModel
    with {
  TrainingRecord({
    required this.id,
    required this.type,
  });

  @override
  @Supabase(unique: true)
  @Sqlite(index: true, unique: true)
  final String id;
  @Supabase(enumAsString: true)
  @Sqlite(enumAsString: true)
  final TrainingType? type;
}

Here is the generate code:

Future<TrainingRecord> _$TrainingRecordFromSupabase(Map<String, dynamic> data,
    {required SupabaseProvider provider,
    OfflineFirstWithSupabaseRepository? repository}) async {
  return TrainingRecord(
      id: data['id'] as String,
      userId: data['user_id'] as String,
      notes: data['notes'] as String,
      startDate: DateTime.parse(data['start_date'] as String),
      type: TrainingType.values.byName(data['type']));
}

Hence data['type'] is null there is a npe.

My current quick fix is to write a custom generator:

  @Supabase(enumAsString: true, fromGenerator:
  '(%DATA_PROPERTY% == null) ? null : TrainingType.values.byName(%DATA_PROPERTY%)')

Thanks in advance!

@tshedor
Copy link
Collaborator

tshedor commented Jan 1, 2025

@TillJohanndeiter this may be a legacy issue with pre-nullable Dart. For now, add nullable: true to your annotation. I'm hoping to remove the nullable declarations in the next major version of Brick.

@tshedor
Copy link
Collaborator

tshedor commented Jan 2, 2025

Fixed by #521 . Please run dart pub upgrade and ensure your Brick dependencies (brick_json_generators, brick_build, brick_core, and brick_supabase_generators) all update to the latest

@tshedor tshedor closed this as completed Jan 2, 2025
@TillJohanndeiter
Copy link
Author

Fixed by #521 . Please run dart pub upgrade and ensure your Brick dependencies (brick_json_generators, brick_build, brick_core, and brick_supabase_generators) all update to the latest

Works fine. Thank you!

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

No branches or pull requests

2 participants