【问题标题】:Renaming Flutter Freezed field names to snake, pascal,kebab or anothing along those lines将 Flutter Freezed 字段名称重命名为 snake、pascal、kebab 或其他类似的名称
【发布时间】:2022-08-23 20:59:53
【问题描述】:

发布此问题以自己回答。希望这可以帮助某人。

问题:使用颤振 freezed 生成代码,但将 to 和 from json 字段名称更改为特定的重命名模式。

    标签: flutter dart freezed json-serializable


    【解决方案1】:

    解决方案:

    build.yaml

    假设你有一个用freezed 写的课程,就像这样

    import 'package:freezed_annotation/freezed_annotation.dart';
    import 'dart:convert';
    
    part 'my_calss.freezed.dart';
    part 'my_calss.g.dart';
    
    @freezed
    abstract class MyCalss with _$MyCalss {
        const factory MyCalss({
            @required int field1,
            @required String field2,
        }) = _MyCalss;
    
        factory MyCalss.fromJson(Map<String, dynamic> json) => _$MyCalssFromJson(json);
    }
    
    
    

    更改类 my_calss.g.dart 以使用例如 pascal 案例:

    将以下内容放入您的build.yaml

    # targets:
    #   $default:
    #     builders:
    #       freezed:
    #         options:
    #           union_key: type
    #           union_value_case: pascal
    
    targets:
      $default:
        builders:
          json_serializable:
            options:
              # Options configure how source code is generated for every
              # `@JsonSerializable`-annotated class in the package.
              #
              # The default value for each is listed.
              any_map: false
              checked: false
              constructor: ""
              create_factory: true
              create_field_map: false
              create_to_json: true
              disallow_unrecognized_keys: false
              explicit_to_json: false
              field_rename: pascal
              generic_argument_factories: false
              ignore_unannotated: false
              include_if_null: true
    

    感兴趣的主要领域是field_rename,它可以是下面列出的json_serializable 中的枚举FieldRename 中的任何值

    
    /// Values for the automatic field renaming behavior for [JsonSerializable].
    enum FieldRename {
      /// Use the field name without changes.
      none,
    
      /// Encodes a field named `kebabCase` with a JSON key `kebab-case`.
      kebab,
    
      /// Encodes a field named `snakeCase` with a JSON key `snake_case`.
      snake,
    
      /// Encodes a field named `pascalCase` with a JSON key `PascalCase`.
      pascal,
    
      /// Encodes a field named `screamingSnakeCase` with a JSON key
      /// `SCREAMING_SNAKE_CASE`
      screamingSnake,
    }
    

    最初,我发现这个 github 页面here 说要执行以下操作

    However, when I write this, there is an error (when I checked the generated file, it was generated twice)
    @freezed
    @JsonSerializable(fieldRename: FieldRename.snake)
    class Example with _$Example {
      factory Example() = GeneratedClass;
    }
    So rewriting it this way works fine.
    @freezed
    class Example with _$Example {
      @JsonSerializable(fieldRename: FieldRename.snake)
      factory Example() = GeneratedClass;
    }
    

    这些解决方案都没有帮助我,只有编辑 build.yaml 文件解决了这个问题。

    我正在使用的版本

      #Freezed 
      freezed_annotation: ^2.1.0
     #json annotations
      json_annotation: ^4.6.0
    
    dev_dependencies:
      flutter_test:
        sdk: flutter
      build_runner: ^2.2.0
      flutter_lints: ^2.0.1
      freezed: ^2.1.0+1
      json_serializable: ^6.3.1
    
    

    【讨论】:

      猜你喜欢
      • 2014-11-26
      • 1970-01-01
      • 2020-08-20
      • 1970-01-01
      • 1970-01-01
      • 2016-10-19
      • 2020-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多