Json_serializable.dart: Generate better private names for private classes

Created on 7 Mar 2020  路  2Comments  路  Source: google/json_serializable.dart

Hi.

I'm using the package freezed and when I use json_serializable with, this problem shows to me. So I add this line // ignore: non_constant_identifier_names and the problem does not show more. But I need to do it manually, can you add it in your package, will solve this borrying label in poblems tabs.

This is what was generated:

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'ability-model.dart';

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

// ignore: non_constant_identifier_names
_$_Ability _$_$_AbilityFromJson(Map<String, dynamic> json) {
  return _$_Ability(
    name: json['name'] as String,
    url: json['url'] as String,
    isHidden: json['isHidden'] as bool,
    slot: json['slot'] as int,
  );
}

// ignore: non_constant_identifier_names
Map<String, dynamic> _$_$_AbilityToJson(_$_Ability instance) =>
    <String, dynamic>{
      'name': instance.name,
      'url': instance.url,
      'isHidden': instance.isHidden,
      'slot': instance.slot,
    };

And it's the code:

import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:flutter/foundation.dart';

part 'ability-model.freezed.dart';
part 'ability-model.g.dart';

@freezed
abstract class Ability with _$Ability {
  const factory Ability({
    @required String name,
    @required String url,
    @required bool isHidden,
    @required int slot,
  }) = _Ability;

  factory Ability.fromJson(Map<String, dynamic> json) =>
      _$AbilityFromJson(json);
}

I don't know if I'm doing something wrong, if so tell me what I need to do to solve this, thanks.

Next breaking change release P2 medium enhancement

Most helpful comment

Add source_gen to your pubspec: https://github.com/dart-lang/pubspec_parse/commit/a5521d8e821fce1e41d13087e1ae2524ea3ad726#diff-fb2d52cd953afedcafc9fe7629c6f132R24

Add to your build.yaml (or create a build.yaml)

https://github.com/dart-lang/pubspec_parse/commit/a5521d8e821fce1e41d13087e1ae2524ea3ad726#diff-5b123ecec7bf9d216a1323f790a0602aR15

All 2 comments

JsonSerializable should be smarter when creating the private name for classes that are already private.

_SomeClass -> _$_SomeClass is not ideal
We should do _SomeClass -> _$SomeClass

In the meantime, you can disable the lint in generated code.

Look at this Commit for an example: https://github.com/dart-lang/pubspec_parse/commit/a5521d8e821fce1e41d13087e1ae2524ea3ad726

Add source_gen to your pubspec: https://github.com/dart-lang/pubspec_parse/commit/a5521d8e821fce1e41d13087e1ae2524ea3ad726#diff-fb2d52cd953afedcafc9fe7629c6f132R24

Add to your build.yaml (or create a build.yaml)

https://github.com/dart-lang/pubspec_parse/commit/a5521d8e821fce1e41d13087e1ae2524ea3ad726#diff-5b123ecec7bf9d216a1323f790a0602aR15

Was this page helpful?
0 / 5 - 0 ratings

Related issues

enyo picture enyo  路  6Comments

Nico04 picture Nico04  路  5Comments

fengqiangboy picture fengqiangboy  路  5Comments

xiang23 picture xiang23  路  5Comments

AlexBigCheese picture AlexBigCheese  路  3Comments