to_string_helper 1.1.1 to_string_helper: ^1.1.1 copied to clipboard
Flexibly configure output of toString method (with or without code generation) (with or without mixin).
Simple example (without code generator) #
-
pubspec.yaml
dependencies: to_string_helper: ^1.1.0
-
bike.dart
import 'package:to_string_helper/to_string_helper.dart'; class Bike { final hasEngine = false; final wheels = 2; @override String toString() { return ToStringHelper(this) .add('wheels', wheels) // named value .addValue(hasEngine) // unnamed value .toString(); } }
-
Output
Bike{wheels=2, false}
Simple example (with code generator) #
- Require package to_string_helper_generator
pubspec.yaml
dependencies: to_string_helper: ^1.1.0 dev_dependencies: build_runner: ^1.0.0 to_string_helper_generator: ^1.1.0
bike.dart
with mixinimport 'package:to_string_helper/to_string_helper.dart'; part 'bike.g.dart'; // Name of the generated mixin is in format _$<ClassName>ToString @ToStringMixin() class Bike with _$BikeToString { final hasEngine = false; @ToStringField(exclude: true) final wheels = 2; }
bike.dart
without mixinimport 'package:to_string_helper/to_string_helper.dart'; part 'bike.g.dart'; @ToString() class Bike { final hasEngine = false; @ToStringField(exclude: true) final wheels = 2; @override String toString() { // Name of the generated method is in format _$<className>ToString() return _$bikeToString(this); } }
- Generate code (see build_runner)
pub run build_runner build
orflutter packages run build_runner build
: run a single build and exit.pub run build_runner watch
orflutter packages run build_runner watch
: continuously run builds as you edit files.
- Output
Bike{hasEngine=false}