to_string 1.1.0

  • Readme
  • Changelog
  • Example
  • Installing
  • new50

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.1.0

dev_dependencies:
  to_string_generator: ^1.1.0
  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);
  }
}

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);
  }
}

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

1.1.0 #

  • Private or static field and getter with ToString() will be shown in method toString()

  • Public field, private or static field and getter with ToString() in supper class and mixin class with ToString(), will be shown in their base class method toString

1.0.1 #

  • Fix bug setter is generated incorrectly.

1.0.0 #

  • Annotate ToString() to getter to let it show in toString()

0.1.0 #

  • Stable release

0.0.1 #

  • Add ToString annotation for generating toString()

example/example.dart

import 'package:to_string/to_string.dart';

part 'example.g.dart';

@ToString()
class Cat {
  Cat({this.color, this.weight, this.wings});

  final classify = "Animal";

  String color;

  double weight;

  String wings;

  @ToString()
  bool get hasWings => wings != null;

  /// toString() will return like this:
  ///
  /// ```dart
  /// 'Cat{classify: Animal, color: white, weight: 1.14, wings: null, hasWings: false}';
  /// ```
  @override
  String toString() {
    return _$CatToString(this);
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  to_string: ^1.1.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

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

3. Import it

Now in your Dart code, you can use:


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

We analyzed this package on Nov 7, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.0
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:to_string/to_string.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.0 <3.0.0
Dev dependencies
pedantic ^1.8.0
test ^1.6.0