Is there a way to put the generated files in a separate folder in flutter?
Asked Answered
H

2

20

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?

Hedgehop answered 30/6, 2021 at 7:17 Comment(1)
Did you find the way to do that?Seidel
P
5

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

Pestana answered 7/11, 2021 at 20:49 Comment(4)
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 successfulCelebrity
This creates an entire project inside the output folder and not just the generated filesPedlar
U
6

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);
}

Upshaw answered 4/3, 2023 at 17:4 Comment(0)
P
5

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

Pestana answered 7/11, 2021 at 20:49 Comment(4)
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 successfulCelebrity
This creates an entire project inside the output folder and not just the generated filesPedlar

© 2022 - 2024 — McMap. All rights reserved.