auto_model 1.0.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 52

A library that generate some standard functionality and utility functions for Model(similar to Data Class in Kotlin)

annotations #

  • @Json() / @json : An annotation that add an extended json field to the model to convert the current model instance into standard JSON.

  • @SerializedName('name'') : An annotation that indicates this member should be serialized to JSON with the provided name value as its field name(similar to SerializedName in Gson).

  • @Copy() / @copy : An annotation that add an extended function to the model to copy an object altering some of its properties.

  • @ToString() / @toString : An annotation that add an extended function (named toString2) to generate a string with form like "User(name=John, age=42)".

Usage #

  • step 1: Define a model class and add one or more of @Json() / @json || @Copy() / @copy || @ToString() / @toString to the model class.

class.dart:

import 'package:auto_model/auto_model.dart';

import 'student.dart';
import 'teacher.dart';

part 'class.g.dart';

@json
@toString
@copy
class Class {
  String name;
  List<Student> students;
  Teacher headTeacher;

  int get studentCount => students?.length;

  /// required empty ctr if use @toString
  Class();

  Class.require(this.name, this.headTeacher, {this.students});
}

  • step 2: In module directory, run:
$ pub get
...
$ pub run build_runner build
  • When the command finishes executing, a dart standard source code file with a suffix of *.g.dart will be generated. The code will generate an extension class that contains the corresponding function or field, as follows:

class.g.dart

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'class.dart';

// **************************************************************************
// CopyGenerator
// **************************************************************************

extension $CopyClassExtension on Class {
  /// Used for copy an object altering some of its properties,
  /// but keeping the rest unchanged.
  Class copy({String name, List<Student> students, Teacher headTeacher}) {
    var copied = Class(); // ignore: prefer_final_locals
    if (name != null) {
      copied.name = name;
    } else {
      copied.name = this.name;
    }

    if (students != null) {
      copied.students = students;
    } else {
      copied.students = this.students;
    }

    if (headTeacher != null) {
      copied.headTeacher = headTeacher;
    } else {
      copied.headTeacher = this.headTeacher;
    }

    return copied;
  }
}

// **************************************************************************
// JsonGenerator
// **************************************************************************

extension $JsonClassExtension on Class {
  /// import 'package:auto_model/auto_model.dart';
  String get json => toJson(this);
}

// **************************************************************************
// ToStringGenerator
// **************************************************************************

extension $ToStringClassExtension on Class {
  /// Used for generate a string with all available fields form like
  /// "User(name=John, age=42)".
  String toString2() {
    return 'Class(name=$name, students=$students, headTeacher=$headTeacher, studentCount=$studentCount)';
  }
}


Later plans #

Plan to merge these annotations together and generate them in an extension class

Features and bugs #

Please file feature requests and bugs at the issue tracker.

1.0.0 #

  • Initial version

example/auto_model_example.dart

/// For more usage, please refer to the link:
/// https://github.com/nikeorever/auto_dart/tree/master/auto_model_usage
void main() {
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  auto_model: ^1.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:auto_model/auto_model.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
5
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
52
Learn more about scoring.

We analyzed this package on Feb 27, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.5

Health suggestions

Fix lib/src/copy_generator.dart. (-0.50 points)

Analysis of lib/src/copy_generator.dart reported 1 hint:

line 19 col 43: 'displayName' is deprecated and shouldn't be used. Use getDisplayString instead.

Fix lib/src/internal_generator_mixin.dart. (-0.50 points)

Analysis of lib/src/internal_generator_mixin.dart reported 1 hint:

line 14 col 56: 'name' is deprecated and shouldn't be used. Check element, or use getDisplayString().

Format lib/src/jsons.dart.

Run dartfmt to format lib/src/jsons.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
analyzer >=0.37.0 <0.40.0 0.39.4
build ^1.0.0 1.2.2
source_gen ^0.9.0 0.9.5
Transitive dependencies
_fe_analyzer_shared 1.0.3
args 1.5.2
async 2.4.0
charcode 1.1.3
collection 1.14.12
convert 2.1.1
crypto 2.1.4
csslib 0.16.1
dart_style 1.3.3
glob 1.2.0
html 0.14.0+3
js 0.6.1+1
logging 0.11.4
meta 1.1.8
node_interop 1.0.3
node_io 1.0.1+2
package_config 1.9.1
path 1.6.4
pub_semver 1.4.3
source_span 1.6.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+13
yaml 2.2.0
Dev dependencies
pedantic ^1.8.0 1.9.0
test ^1.6.0