marcelgarus / data_classes Goto Github PK
View Code? Open in Web Editor NEWDart packages that automatically generate data classes for you.
Dart packages that automatically generate data classes for you.
I have this class:
part 'ReminderArgs.g.dart';
@DataClass()
class ReminderArgs {
@nullable String assetId;
@nullable Reminder reminder;
bool showAsset = false;
}
and this is what data_classes generates:
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'ReminderArgs.dart';
// **************************************************************************
// DataClassGenerator
// **************************************************************************
/// This class is the immutable pendant of the MutablerArgs class.
@immutable
class rArgs {
final String assetId;
final Reminder reminder;
final bool showAsset;
/// Default constructor that creates a rArgs.
const rArgs({
this.assetId,
this.reminder,
@required this.showAsset,
}) : assert(showAsset != null);
/// Creates a rArgs from a MutablerArgs.
factory rArgs.fromMutable(MutablerArgs mutable) {
return rArgs(
assetId: mutable.assetId,
reminder: mutable.reminder,
showAsset: mutable.showAsset,
);
}
/// Turns this rArgs into a MutablerArgs.
MutablerArgs toMutable() {
return MutablerArgs()
..assetId = assetId
..reminder = reminder
..showAsset = showAsset;
}
/// Checks if this rArgs is equal to the other one.
bool operator ==(Object other) {
return other is rArgs &&
assetId == other.assetId &&
reminder == other.reminder &&
showAsset == other.showAsset;
}
int get hashCode => hashList([
assetId,
reminder,
showAsset,
]);
rArgs copyWith({
String assetId,
Reminder reminder,
bool showAsset,
}) {
return rArgs(
assetId: assetId ?? this.assetId,
reminder: reminder ?? this.reminder,
showAsset: showAsset ?? this.showAsset,
);
}
String toString() {
return 'rArgs\n'
' assetId: $assetId\n'
' reminder: $reminder\n'
' showAsset: $showAsset\n'
')';
}
}
pubspec.yaml:
dependencies:
data_classes: ^1.1.0
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
flutter_mobx: ^0.3.1+1
mobx: ^0.3.7
package_info: ^0.4.0+6
dev_dependencies:
build_runner: ^1.6.7
data_classes_generator: ^1.1.0
mobx_codegen: ^0.3.6
For a new version of the package, I consider completely overhauling the API of this package.
Fundamental problems of this package
built_value
package, not offering any unique benefits.So, what's the future of this package?
The Dart team actively works on bringing static extension methods and non-nullability by default (NNBD) to Dart. That would allow this package to be even more lightweight and enable a workflow like the following:
Mark your class with an @DataClass()
annotation:
@DataClass()
class User {
final String firstName;
final String lastName;
final String? photoUrl;
const User({
this.firstName,
this.lastName,
this.photoUrl,
});
}
Because of NNBD the constructor is equivalent to the current one; @required
annotations and non-null assertions are no longer needed.
The package could then generate the copy
, ==
, hashCode
and toString
methods as static extension methods on the User
type.
Benefits
copy
method, causing less confusion like #1.Downsides
I'm welcoming any opinions on whether this is a worthy tradeoff.
Marking the non data class with @DataClass
is somewhat counter-intuitive. Rename the annotation to something of the like @GenerateDataClassFor
.
This issue is open for discussion, suggestions are welcome.
Would it be possible to add support for optional named constructor parameters by specifying a default value in the base Mutable class?
For example this class:
@GenerateDataClass()
class MutableState {
String a = "";
String b;
}
Would generate the this constructor:
const ProfileConfirmState({
this.a = "",
@required this.b
}) : assert(a != null),
assert(b != null);
hi,
I'm looking into your example and I guess nulls do not work properly(I didn't run it though).
var me = const User(firstName: 'Marcel', lastName: 'Garus', photoUrl: 'http://example.com');
var mySister = me.copyWith(firstName: 'Yvonne', photoUrl: null);
photoUrl
is updated as follows photoUrl: photoUrl ?? this.photoUrl,
which leads to mySister.photoUrl == 'http://example.com'
instead of null.
Thanks for sharing this library! I have definitely been wanting to use something like this.
Is it possible to add json serialization to the generated data class?
My specific use-case is that I'd like to use json_serializable. To use it, I'd need to add some boilerplate (toJson
and fromJson
) to the generated data class; however, the generated data class can't be edited by hand.
In a few of my projects, I have an enum that represents a class's "type" or kind (not in the Dart Type kind of way). It would be nice to let a package generate the value getters.
Take the following class:
enum Color { red, green, yellow, blue }
enum FruitShape { round, curved }
@GenerateDataClassFor()
class MutableFruit {
@GenerateValueGetters()
Color color;
@GenerateValueGetters(usePrefix=true)
FruitShape shape;
Fruit(this.color, this.shape);
}
class Banana extends Fruit {
Banana() : super(Color.yellow, FruitShape.curved);
}
class Blueberry extends Fruit {
Blueberry() : super(Color.blue, FruitShape.round);
}
It would be cool if the Fruit
had some getters like the following:
class Fruit {
...
bool get isRed => color == Color.red;
bool get isGreen => color == Color.green;
...
bool get isShapeRound => shape == FruitShape.round;
bool get isShapeCurved => shape == FruitShape.curved;
}
Why? Because then we could use some cool stuff like Banana().isYellow
.
Feedback about this proposal is very welcome.
Would it be possible to add support for excluding fields from the generated copyWith, hashCode/== methods?
For example:
class MutableState({
Exclude(copyWith == true, equals == true) String a;
String b;
})
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.