In my app, I use freezed with json_serializable. In the folder, where I put all my data models a lot of generated files, like model.g.dart
, model.freeezed.dart
. Is there any possibility to move all generated files like that to a separate folder in the project, like generated
?
Is there a way to put the generated files in a separate folder in flutter?
Asked Answered
Did you find the way to do that? –
Seidel
Accordint to the build_runner documentation
flutter pub run build_runner build --output <dirname>
or
flutter pub run build_runner build --output models:<directory name>
to output only the models directory
Can't figure out how to use it. I have file lib/model.dart and I want file model.g.dart to be inside /lib/adapters/ directory, but
build --output adapters
creates packages in the root of my project with test, packages etc... –
Celebrity @Celebrity did you try to pass /lib/adapters instead of adapters? –
Pestana
Yes, I tried lib/adapters, /lib/adapters, ./lib/adapters but was not successful –
Celebrity
This creates an entire project inside the output folder and not just the generated files –
Pedlar
Here's working example from stackoveflow:
Basically create build.yaml file like this, it will generate files in generated
folder:
targets:
$default:
builders:
source_gen|combining_builder:
generate_for:
- lib/**.dart
options:
build_extensions:
'^lib/{{}}.dart': 'lib/generated/{{}}.g.dart'
freezed|freezed:
enabled: true
options:
build_extensions:
'^lib/{{}}.dart': 'lib/generated/{{}}.freezed.dart'
Don't forget to add generated
to part
.
import 'package:freezed_annotation/freezed_annotation.dart';
part 'generated/person.g.dart';
part 'generated/person.freezed.dart';
@freezed
class Person with _$Person {
const factory Person({
required String firstName,
required String lastName,
required int age,
}) = _Person;
factory Person.fromJson(Map<String, Object?> json)
=> _$PersonFromJson(json);
}
Accordint to the build_runner documentation
flutter pub run build_runner build --output <dirname>
or
flutter pub run build_runner build --output models:<directory name>
to output only the models directory
Can't figure out how to use it. I have file lib/model.dart and I want file model.g.dart to be inside /lib/adapters/ directory, but
build --output adapters
creates packages in the root of my project with test, packages etc... –
Celebrity @Celebrity did you try to pass /lib/adapters instead of adapters? –
Pestana
Yes, I tried lib/adapters, /lib/adapters, ./lib/adapters but was not successful –
Celebrity
This creates an entire project inside the output folder and not just the generated files –
Pedlar
© 2022 - 2024 — McMap. All rights reserved.