rrousselGit / freezed

Code generation for immutable classes that has a simple syntax/API without compromising on the features.
https://pub.dev/packages/freezed
1.91k stars 235 forks source link

Bloc with freezed not working. #1064

Closed Vineeth-Kolichal closed 5 months ago

Vineeth-Kolichal commented 5 months ago

Describe the bug I am using Bloc with freezed for managing states of my flutter project. But when I am trying to create a bloc today the .freezed file is not generating, I run build runner command multiple times, even though its not generating

rrousselGit commented 5 months ago

Please be more specific and provide a complete example.

anugrahsputra commented 5 months ago

I also have this issue, the .freezed.dart file is not generated for bloc/cubit.

➜ dart run build_runner build --delete-conflicting-outputs
[INFO] Generating build script completed, took 1.0s
[INFO] Reading cached asset graph completed, took 2.3s
[INFO] Checking for updates since last build completed, took 5.1s
[WARNING] No actions completed for 16.2s, waiting on:
  - freezed on test/helper/mock.dart
  - freezed on lib/features/ayah/logic/blocs/ayahs/ayahs_bloc.dart
  - freezed on lib/features/bookmark/logics/blocs/bookmark/bookmark_bloc.dart
  - freezed on lib/features/detail_surah/logic/blocs/detail_surah/detail_surah_bloc.dart
  - freezed on lib/features/detail_surah/logic/cubits/verse_audio/verse_audio_cubit.dart
  .. and 4 more

[WARNING] No actions completed for 48.0s, waiting on:
  - freezed on test/helper/mock.dart
  - freezed on lib/features/ayah/logic/blocs/ayahs/ayahs_bloc.dart
  - freezed on lib/features/bookmark/logics/blocs/bookmark/bookmark_bloc.dart
  - freezed on lib/features/detail_surah/logic/blocs/detail_surah/detail_surah_bloc.dart
  - freezed on lib/features/detail_surah/logic/cubits/verse_audio/verse_audio_cubit.dart
  .. and 4 more

[INFO] Running build completed, took 1m 54s
[INFO] Caching finalized dependency graph completed, took 1.6s
[INFO] Succeeded after 1m 56s with 83 outputs (342 actions)

image

rrousselGit commented 5 months ago

I need a complete example; something I can run.

anugrahsputra commented 5 months ago

not sure what you mean, but i guess to explain the issue is when creating bloc with freezed, you would do this

// state
part of 'example_bloc.dart';

@freezed
class ExampleState with _$ExampleState {
  const factory ExampleState.initial() = _Initial;
}
// event
part of 'example_bloc.dart';

@freezed
class ExampleEvent with _$ExampleEvent {
  const factory ExampleEvent.started() = _Started;
}

then you run dart run build_runner build to generate example_bloc.freezed.dart

but like @Vineeth-Kolichal said, .freezed file was not generated and that's the issue.

sorry for poor explanation

raj457036 commented 5 months ago

Version 2.4.7 works as expected

thanhminhkma commented 5 months ago

Version 2.4.7 works as expected

Version 2.4.7 working properly with me too.

Yegair commented 5 months ago

I have encountered the same problem, I am able to reproduce it with Freezed 2.4.8 as well as 2.5.0. Created a minimal example: https://github.com/Yegair/freezed-bloc-error-reproduction

It contains 3 Files

main.dart

void main(List<String> arguments) {
  final myCubit = MyCubit();

  try {
    myCubit.doSomething();
  } finally {
    myCubit.close();
  }
}

cubit.dart

import 'package:bloc/bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'state.dart';
part 'cubit.freezed.dart';

class MyCubit extends Cubit<MyState> {
  MyCubit() : super(MyState.initial());

  void doSomething() {
    emit(MyState.didSomething());
  }
}

state.dart

part of 'cubit.dart';

@freezed
sealed class MyState with _$MyState {
  const MyState._();
  const factory MyState.initial() = MyStateInitial;
  const factory MyState.didSomething() = MyStateDidSomething;
}

I then try to generate the cubit.freezed.dart file as usual like

dart run build_runner build --delete-conflicting-outputs

The build runner seems to do something (there are some files produced in the .dart_tool directory), but the expected file cubit.freezed.dart is not being generated.

Reverting to freezed: 2.4.7 reliably fixes the issue.

specOper99 commented 5 months ago

It is happening when the @freezed annotation is inside the part file *_event.dart or *_state.dart

AngeloAvv commented 5 months ago

Same problem here. Reverting to 2.4.7 works properly. I don't know if it's something related to other generators, here's some of my dev deps:

  flutter_test:
    sdk: flutter
  integration_test:
    sdk: flutter
  flutter_driver:
    sdk: flutter
  auto_route_generator: ^8.0.0
  build_runner: ^2.4.9
  drift_dev: ^2.16.0
  flutter_lints: ^3.0.2
  freezed: 2.4.7
  retrofit_generator: ^8.1.0
  json_serializable: ^6.7.1
  mockito: ^5.4.4
  bloc_test: ^9.1.7
  fake_cloud_firestore: ^2.4.9
  pigeon: ^18.0.0
  data_fixture_dart: ^2.2.0
  url_launcher_platform_interface: ^2.3.2
  plugin_platform_interface: ^2.1.8
  mocktail: ^1.0.3
Lzyct commented 5 months ago

Same issue, need to revert to version 2.4.7

YawarOsman commented 5 months ago

Same here, and version 2.4.7 still not working

Lzyct commented 5 months ago

Same here, and version 2.4.7 still not working

You should force it to use version 2.4.7 by remove ^

Should be like this

freezed: 2.4.7
YawarOsman commented 5 months ago

Ok, worked thanks

TheFabbiusCorp commented 5 months ago

Same same. Probably something at this commit broke the generator: https://github.com/rrousselGit/freezed/commit/c5377a9fc1b876fd617bc1d1469348ac109e05e7.

lvsecoto commented 5 months ago

Sam problem, with a freezed class in part file.

lvsecoto commented 5 months ago

Sam problem, with a freezed class in part file.

YannMLD commented 5 months ago

I had the same problem when I upgraded to 2.5.0 and had to go back to 2.4.7 to get Bloc working again

PrzemyslawPluszowy commented 5 months ago

i had the same issue, im back to 2.4.7 and still don't work

YannMLD commented 5 months ago

i had the same issue, im back to 2.4.7 and still don't work

Did you follow this https://github.com/rrousselGit/freezed/issues/1064#issuecomment-2041186153 correctly ?

PrzemyslawPluszowy commented 5 months ago

yes, freezed: 2.4.7 normal class are generated good, but state no

PrzemyslawPluszowy commented 5 months ago

ok flutter cache repair fix problem on 2.4.7

Arenukvern commented 5 months ago

for my case it seems like freezed ignores any "part of" files and can generate .freezed only if there is one file

what doesn't work:

models.dart have 
part 'file_1.dart';
part 'file_2.dart';
part 'models.freezed.dart';

file_1.dart have part of 'models.dart';
file_2.dart have part of 'models.dart';

if separate files to independent files, it works:

file_1.dart have only part 'file_1.freezed.dart';
file_2.dart have only part 'file_2.freezed.dart';

downgrading to 2.4.7 solves the issue

rrousselGit commented 5 months ago

Fixed in 2.5.1