-
Notifications
You must be signed in to change notification settings - Fork 28.9k
Open
Labels
P2Important issues not at the top of the work listImportant issues not at the top of the work listc: new featureNothing broken; request for a new capabilityNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to FlutterA detailed proposal for a change to Fluttercustomer: crowdAffects or could affect many people, though not necessarily a specific customer.Affects or could affect many people, though not necessarily a specific customer.dependency: dartDart team may need to help usDart team may need to help usframeworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.team-frameworkOwned by Framework teamOwned by Framework teamtriaged-frameworkTriaged by Framework teamTriaged by Framework team
Description
Use case
Code generation has became a big part of flutter development, I'm currently using it for:
- Generating i18n strings.
- Generating asset paths.
- Generating json parsing.
- Generating moor database handling.
Unfortunately, the experience related to code generation is not as good as it should be:
- Code generated files are checked by analyzer for styling problems.
- I need to setup build_runner for every project and tell people how to use it.
- I need to remember to run build_runner.
- Build runner takes many seconds to build.
- During build process, generated code gets deleted, and analyzer errors are being displayed.
- Using a code generator requires boilerplate setup for each class:
a. Adding part directive.
b. Adding factory methods that delegate constructors to the generated class. - Generated files pollute my project's workspace, project wide text search returns results from generated code.
- Analysis exclude does not work properly: The "exclude" list in .analysis_options is ignored by the dartanalyzer cli dart-lang/sdk#25551, and if it worked properly, then I would not see errors.
I am aware that some of these problems have partial solutions, such as using snippets, configuring IDE to run build_runner task, and so on. I just don't think it's good enough if everyone who uses code generation needs to do it on their own.
Proposal
Code generation should be completely seamless. Below are my suggestions that would achieve this. I realize that some of them may be infeasible.
- Code generation should be integral part of build process. IDE must generate code automatically and keep it up to date.
- All generated code should be send to build cache, unless I'm publishing a package.
- Analyzer should not show any errors if code is not generated.
- Analyzer should show generation errors. The errors should be as descriptive as possible.
- Code generation should be fast.
- It should be possible to code generate static methods and costructors for the annotated class.
- 'part' directive should not be needed. Adding annotation should be enough.
- If there are any errors in generated code, they should be treated as code generation errors. Styling issues should be ignored completely.
Example
Using json_serializable as example, here is current state of things:
dependencies:
json_annotation: ^3.0.1
dev_dependencies:
build_runner: ^1.8.1
json_serializable: ^3.3.0
import 'package:json_annotation/json_annotation.dart';
part 'example.g.dart';
@JsonSerializable(nullable: false)
class Person {
final String firstName;
final String lastName;
final DateTime dateOfBirth;
Person({this.firstName, this.lastName, this.dateOfBirth});
factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
Map<String, dynamic> toJson() => _$PersonToJson(this);
}
And here is what I would like to write:
builders:
json_serializable: ^3.3.0
import 'package:json_annotation/json_annotation.dart';
@JsonSerializable(nullable: false)
class Person {
final String firstName;
final String lastName;
final DateTime dateOfBirth;
}
Benefits
- Parsing jsons will become pleasant experience.
- People will start writing smaller code generators that were too infeasible/cumbersome before.
- People will start experimenting with different approaches to flutter. See: Reusing state logic is either too verbose or too difficult #51752 (comment)
- Inexperienced flutter devs will be able to use it.
Links
ifiokjr, twister21, azbuky, TimWhiting, jogboms and 102 moresamandmoore, tbm98, jeanherfs, listepo, tomgilder and 6 moresamandmoore, tbm98, jeanherfs, listepo, tomgilder and 11 more
Metadata
Metadata
Assignees
Labels
P2Important issues not at the top of the work listImportant issues not at the top of the work listc: new featureNothing broken; request for a new capabilityNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to FlutterA detailed proposal for a change to Fluttercustomer: crowdAffects or could affect many people, though not necessarily a specific customer.Affects or could affect many people, though not necessarily a specific customer.dependency: dartDart team may need to help usDart team may need to help usframeworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.team-frameworkOwned by Framework teamOwned by Framework teamtriaged-frameworkTriaged by Framework teamTriaged by Framework team