Freezed: [Suggestion] VS Code snippet to generate basic layout of freezed model class

Created on 3 Mar 2020  ·  6Comments  ·  Source: rrousselGit/freezed

It would be useful to have a VS Code snippet to generate basic layout of the model class. It's easy to forget about some parts such as part or abstract.

Did anyone create such snippet? I would love to use one.

Most helpful comment

Here are all the snippets which you may find useful when working with Freezed.

"Part statement": {
    "prefix": "pts",
    "body": [
      "part '${TM_FILENAME_BASE}.g.dart';",
    ],
    "description": "Creates a filled-in part statement"
  },
  "Part 'Freezed' statement": {
    "prefix": "ptf",
    "body": [
      "part '${TM_FILENAME_BASE}.freezed.dart';",
    ],
    "description": "Creates a filled-in freezed part statement"
  },
  "Freezed Data Class": {
    "prefix": "fdataclass",
    "body": [
      "@freezed",
      "abstract class ${1:DataClass} with _$${1:DataClass}{",
      "  const factory ${1:DataClass}(${2}) = _${1:DataClass};",
      "}"
    ],
    "description": "Freezed Data Class"
  },
  "Freezed Union": {
    "prefix": "funion",
    "body": [
      "@freezed",
      "abstract class ${1:Union} with _$${1:Union}{",
      "  const factory ${1:Union}.${2}(${4}) = ${3};",
      "}"
    ],
    "description": "Freezed Union"
  },
  "Freezed Union Case": {
    "prefix": "funioncase",
    "body": [
      "const factory ${1:Union}.${2}(${4}) = ${3};"
    ],
    "description": "Freezed Union Case"
  },
  "From JSON": {
    "prefix": "fromJson",
    "body": [
      "factory ${1}.fromJson(Map<String, dynamic> json) => _$${1}FromJson(json);"
    ],
    "description": "From JSON"
  },

All 6 comments

I think @ResoCoder did

Ok, I tried on my own and this is my proposal:

    "Generate freezed class": {
        "prefix": "freezed",
        "body": [
            "import 'package:freezed_annotation/freezed_annotation.dart';",
            "",
            "part '$1.freezed.dart';",
            "",
            "@freezed",
            "abstract class $2 with _$$2 {",
            "factory $2() = _$2;",
            "}"
        ]
    }

Here are all the snippets which you may find useful when working with Freezed.

"Part statement": {
    "prefix": "pts",
    "body": [
      "part '${TM_FILENAME_BASE}.g.dart';",
    ],
    "description": "Creates a filled-in part statement"
  },
  "Part 'Freezed' statement": {
    "prefix": "ptf",
    "body": [
      "part '${TM_FILENAME_BASE}.freezed.dart';",
    ],
    "description": "Creates a filled-in freezed part statement"
  },
  "Freezed Data Class": {
    "prefix": "fdataclass",
    "body": [
      "@freezed",
      "abstract class ${1:DataClass} with _$${1:DataClass}{",
      "  const factory ${1:DataClass}(${2}) = _${1:DataClass};",
      "}"
    ],
    "description": "Freezed Data Class"
  },
  "Freezed Union": {
    "prefix": "funion",
    "body": [
      "@freezed",
      "abstract class ${1:Union} with _$${1:Union}{",
      "  const factory ${1:Union}.${2}(${4}) = ${3};",
      "}"
    ],
    "description": "Freezed Union"
  },
  "Freezed Union Case": {
    "prefix": "funioncase",
    "body": [
      "const factory ${1:Union}.${2}(${4}) = ${3};"
    ],
    "description": "Freezed Union Case"
  },
  "From JSON": {
    "prefix": "fromJson",
    "body": [
      "factory ${1}.fromJson(Map<String, dynamic> json) => _$${1}FromJson(json);"
    ],
    "description": "From JSON"
  },

I've written Live Templates I've written for Android Studio/Intelli.
In case someone wants to use it:

<template name="ffreezed" value="import 'package:flutter/foundation.dart';&#10;import 'package:freezed_annotation/freezed_annotation.dart';&#10;&#10;part '$FILE_NAME$.freezed.dart';&#10;part '$FILE_NAME$.g.dart';&#10;&#10;@freezed&#10;abstract class $CLASS_NAME$ with _$$$CLASS_NAME$ {&#10;&#10;  const factory $CLASS_NAME$({}) = _$CLASS_NAME$;&#10;  &#10;  factory $CLASS_NAME$.fromJson(Map&lt;String, dynamic&gt; json) =&gt; _$$$CLASS_NAME$FromJson(json);&#10;&#10;}" description="Freezed immutable file with imports and parts" toReformat="false" toShortenFQNames="true">
  <variable name="FILE_NAME" expression="" defaultValue="" alwaysStopAt="true" />
  <variable name="CLASS_NAME" expression="" defaultValue="" alwaysStopAt="true" />
  <context>
    <option name="DART" value="true" />
  </context>
</template>
<template name="freezed" value="&#10;@freezed&#10;abstract class $CLASS_NAME$ with _$$$CLASS_NAME$ {&#10;&#10;  const factory $CLASS_NAME$({}) = _$CLASS_NAME$;&#10;  &#10;  factory $CLASS_NAME$.fromJson(Map&lt;String, dynamic&gt; json) =&gt; _$$$CLASS_NAME$FromJson(json);&#10;&#10;}" description="Freezed class without imports and part declaration" toReformat="false" toShortenFQNames="true">
  <variable name="CLASS_NAME" expression="" defaultValue="" alwaysStopAt="true" />
  <context>
    <option name="DART" value="true" />
  </context>
</template>

Or just the raw code templates:

@freezed
abstract class $CLASS_NAME$ with _$$$CLASS_NAME$ {

  const factory $CLASS_NAME$({}) = _$CLASS_NAME$;

  factory $CLASS_NAME$.fromJson(Map<String, dynamic> json) => _$$$CLASS_NAME$FromJson(json);

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

part '$FILE_NAME$.freezed.dart';
part '$FILE_NAME$.g.dart';

@freezed
abstract class $CLASS_NAME$ with _$$$CLASS_NAME$ {

  const factory $CLASS_NAME$({}) = _$CLASS_NAME$;

  factory $CLASS_NAME$.fromJson(Map<String, dynamic> json) => _$$$CLASS_NAME$FromJson(json);

}

Closing this since there are quite a few solutions and I don't want to maintain these myself.

Was this page helpful?
0 / 5 - 0 ratings