0 purchases
to string
to_string #
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
copied to clipboard
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);
}
}
copied to clipboard
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);
}
}
copied to clipboard
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);
}
}
copied to clipboard
Pretty Print Supports #
Pretty print supports nested class indent!
Cat{
classify: Animal,
color: red,
weight: 12.0,
wings: has,
ball: Ball{ <= nested
color: red,
},
}
copied to clipboard
There are two ways you can enable pretty print:
Use ToString() annotation:
ToString(
prettyPrint: true,
// default to " "
indent: " ",
)
/// class Cat {...
copied to clipboard
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: " "
copied to clipboard
Lastly, we use build_runner!
In flutter
flutter packages pub run build_runner build
copied to clipboard
In dart
pub run build_runner build
copied to clipboard
Features and bugs #
Please file feature requests and bugs at the Github Issue Tracker.
Github Issue tracker: https://github.com/lvsecoto/to_string/issues
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.