Checkout apps I've created with others under my portfolio.
jarekb123 / dart_dataclass Goto Github PK
View Code? Open in Web Editor NEWDart data class (equals, hashCode, toString, copyWith) methods generator
License: MIT License
Dart data class (equals, hashCode, toString, copyWith) methods generator
License: MIT License
Checkout apps I've created with others under my portfolio.
Because every version of flutter_test from sdk depends on collection 1.15.0-nullsafety and dataclass 0.3.0+1 depends on collection >=1.14.0 <1.15.0, flutter_test from sdk is incompatible with dataclass 0.3.0+1.
And because no versions of dataclass match >0.3.0+1 <0.4.0, flutter_test from sdk is incompatible with dataclass ^0.3.0+1.
Hello,
I've found that in some situations it is needed to inherit another abstract class. Therefore the generated base class is not always appropriate. Instead, do generate a mixing.
Cheers,
If you declare a field as follows:
class SomeDataClass _$SomeDataClass {
final List<SomeType> someTypes;
}
The generated code looks like that:
_$SomeDataClass {
final List someTypes
}
instead of
_$SomeDataClass {
final List<SomeType> someTypes;
}
In particular, the build
and collection
plugins:
Example
@dataClass
class PaginationMeta extends _$PaginationMeta {
final int page;
final int total;
final int perPage;
bool get hasMorePages => perPage * page < total;
PaginationMeta({this.page, this.total, this.perPage});
}
Generated code
abstract class _$PaginationMeta {
const _$PaginationMeta();
int get page;
int get total;
int get perPage;
bool get hasMorePages;
bool operator ==(other) {
if (identical(this, other)) return true;
if (other is! PaginationMeta) return false;
return true &&
this.page == other.page &&
this.total == other.total &&
this.perPage == other.perPage &&
this.hasMorePages == other.hasMorePages;
}
int get hashCode {
return mapPropsToHashCode([page, total, perPage, hasMorePages]);
}
String toString() {
return 'PaginationMeta <\'page\': ${this.page},\'total\': ${this.total},\'perPage\': ${this.perPage},\'hasMorePages\': ${this.hasMorePages},>';
}
PaginationMeta copyWith(
{int page, int total, int perPage, bool hasMorePages}) {
return PaginationMeta(
page: page ?? this.page,
total: total ?? this.total,
perPage: perPage ?? this.perPage,
hasMorePages: hasMorePages ?? this.hasMorePages, // THIS SHOULD NOT BE USED
);
}
}
Currently library fails if class has at any static fields (which should not affect data class at all).
For ex.
@dataClass
class Contact extends _$Contact {
static const unknownName = '{empty}';
final String name;
}
Such example throws an error:
[SEVERE] dataclass_generator:dataclass on lib/contact.dart:
@DataClass should have final fields only
I should enable DeepCollectionEquality
by default, without adding @Collection
annotation
It would be nice if one can ignore some features if needed. For instance, take the code bellow:
import 'package:flutter/material.dart';
import './passage.dart';
part 'search_result.g.dart';
@DataClass(withCopy: false)
@immutable
class SearchResult {
final String query;
final int page;
@DataClassField(equal: false)
final int totalHits;
@DataClassIgnore
final List<Passage> hits;
SearchResult({this.query, this.page, this.totalHits, this.hits});
}
It represents a search query, if query
and page
are equals I don't need to check other fields to know it's the same value as other
.
It would also be nice to ignore at class level, like copyWith
.
Hi.
The package looks useful and simple. I am wondering how it compares to built_value. Basically, I want to replace built_value with this package and the class properties be all final. This way I can have immutable classes with equals and copyWith. And with less code than with built_value.
Am I missing something? Are there any thing to consider? I suppose I have to continue using BuiltList instead of List right?
Thanks.
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.