to_string 1.2.1 copy "to_string: ^1.2.1" to clipboard
to_string: ^1.2.1 copied to clipboard

A tools for generating toString method for class, based on build_runner

to_string #

Pub Package Build Status

A tool for generating toString method for class, based on build_runner.

Who uses this tool #

Someone who is boring to write and maintain the method toString()

Installation #

Add dependencies in your pubspec.yaml:

dependencies:
  to_string: ^1.2.1

dev_dependencies:
  to_string_generator: ^1.2.1
  build_runner: ^1.7.1

Usage #

In class you want to write toString() method:

  • Annotate the class with ToString()
  • Override the toString method.
/// cat.dart
import 'package:to_string/to_string.dart';

part 'cat.g.dart';

@ToString()
class Cat {
  Cat(this.color, this.weight);
  
  String color;
  double weight;
  
  @override
  String toString() {
    // [_$CatToString] is generated at `cat.g.dart`,
    // and it returns likes "Cat{color: white, weight: 1.2}"
    return _$CatToString(this);
  }
}

By default, getter, static field, and private field will not be shown in toString. But you can use ToString to make them show.

/// cat.dart
import 'package:to_string/to_string.dart';

part 'cat.g.dart';

@ToString()
class Cat {
  
  @ToString()
  static int leg = 4;
  
  Cat(this.color, this.weight, this.wings);
  
  String color;
  double weight;
  String wings;
  
  String _heart = "warm";
  
  @ToString()
  bool get hasWings => wings != null;
  
  @override
  String toString() {
    // [_$CatToString] is generated at `cat.g.dart`,
    // and it returns likes this: 
    // "Cat{leg: Cat.leg, color: white, weight: 1.2, wings: null, _heart: warm, hasWings: false}"
    return _$CatToString(this);
  }
}

Supper Class And Mixin #

If you annotate ToString() to the class`s supper class or mixin, their field (public field and other field with ToString()) will be include in the base class method toString.

/// cat.dart
@ToString()
class Animal {
  bool needOxygen = true;
}

@ToString()
class Rocket {
  bool canFly = true;
}

@ToString()
class Cat extends Animal with Rocket{
  
  String name;
  
  @override
  String toString() {
    // [_$CatToString] is generated at `cat.g.dart`,
    // and it returns likes this: 
    // "Cat{needOxygen: true, canFly: true, name: kitty}"
    return _$CatToString(this);
  }
}

Pretty Print Supports #

Pretty print supports nested class indent!

Cat{
  classify: Animal,
  color: red,
  weight: 12.0,
  wings: has,
  ball: Ball{         <= nested
    color: red,
  },
}

There are two ways you can enable pretty print:

Use ToString() annotation:

ToString(
    prettyPrint: true,

    // default to "  "
    indent: "  ",
)
/// class Cat {...

Create a file build.yaml with code and enable all class pretty print:

targets:
  $default:
    builders:
      to_string_generator|to_string:
        options:
          prettyPrint: true
          indent: "  "

Lastly, we use build_runner!

In flutter

flutter packages pub run build_runner build

In dart

pub run build_runner build

Features and bugs #

Please file feature requests and bugs at the Github Issue Tracker.

Github Issue tracker: https://github.com/lvsecoto/to_string/issues

9
likes
40
pub points
39%
popularity

Publisher

unverified uploader

A tools for generating toString method for class, based on build_runner

Repository (GitHub)
View/report issues

License

BSD-3-Clause (license)

More

Packages that depend on to_string