google / google/json_serializable.dart

Allow usage of prefix for the import of json_annotation.dart

Open
#1,115 1 comment 2 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
1.6k
Forks
461
Avg merge
45m
Merged PRs (30d)
1

Description

Hi !
And before all thank you for this package !

I found an issue while using prefix for the import of json_annotation.dart and working with enum.
Say I have a file :

```dart
// user.dart
import 'package:json_annotation/json_annotation.dart' as prefix;

part 'user.g.dart'

@prefix.JsonSerializable()
class User {
final Gender gender;
final String name;
User({required this.gender, required this.name});
}

enum Gender{male, female, other}
```
after running `flutter pub run build_runner watch --delete-conflicting-outputs`
the .g file look like this :
```dart
// GENERATED CODE - DO NOT MODIFY BY HAND

// ignore_for_file: implicit_dynamic_parameter

part of 'user.g.dart';

// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************

User _$UserFromJson(Map json) => User(
name: json['name'] as String,
gender: $enumDecode(_$GenderEnumMap, json['status']),
);

Map _$UserToJson(User instance) => {
'name': instance.name,
'lastName': instance.lastName,
'gender': _$GenderEnumMap[instance.gender],
};

const _$GenderEnumMap = {
Gender.male: 'male',
Gender.female: 'female',
Gender.other: 'other',
};
```
But this will throw `Error: Method not found: '$enumDecode'.`
because `$enumDecode` is define in json_annotation.dart but with the prefix it should be called like that : `prefix.$enumDecode`.

I made a PR to add the prefix before some helpers of json_annotation : #1116

```
Flutter 2.10.2 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 097d3313d8 (3 weeks ago) • 2022-02-18 19:33:08 -0600
Engine • revision a83ed0e5e3
Tools • Dart 2.16.1 • DevTools 2.9.2
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.