freezed icon indicating copy to clipboard operation
freezed copied to clipboard

Handle cases where a named parameter is dynamic and required

Open Haidar0096 opened this issue 3 years ago • 2 comments

I have this class (related to BLoC) :

part of 'playback_bloc.dart';

@freezed
class PlaybackBlocEvent with _$PlaybackBlocEvent {
  const factory PlaybackBlocEvent.init() = _InitEvent;

  const factory PlaybackBlocEvent.startPlayback({
    required String filePath,
    required Function whenFinished,
    required  currentSelectedTrackIndex, // the error turns out to be because I forgot to specify the type here
  }) = _StartPlaybackEvent;

  const factory PlaybackBlocEvent.pausePlayback() = _PausePlaybackEvent;

  const factory PlaybackBlocEvent.resumePlayback() = _ResumePlaybackEvent;

  const factory PlaybackBlocEvent.stopPlayback() = _StopPlaybackEvent;

  const factory PlaybackBlocEvent.playbackEnded() = _PlaybackEndedEvent;
}

After alot of time trying to figure out the error, I discovered that I forgot to specify the type of the parameter currentSelectedTrackIndex , but the runner couldn't find this error. The build runner was generating freezed file with errors and I was not able to run the app, and the error message of the runner is :


[INFO] Generating build script...
[INFO] Generating build script completed, took 749ms

[INFO] Precompiling build script......
[INFO] Precompiling build script... completed, took 13.0s

[INFO] Setting up file watchers...
[INFO] Setting up file watchers completed, took 27ms

[INFO] Waiting for all file watchers to be ready...
[INFO] Waiting for all file watchers to be ready completed, took 176ms

[INFO] Initializing inputs
[INFO] Building new asset graph...
[INFO] Building new asset graph completed, took 1.3s

[INFO] Checking for unexpected pre-existing outputs....
[INFO] Deleting 7 declared outputs which already existed on disk.
[INFO] Checking for unexpected pre-existing outputs. completed, took 22ms

[INFO] Running build...
[INFO] Generating SDK summary...
[INFO] 5.9s elapsed, 0/16 actions completed.
[INFO] Generating SDK summary completed, took 5.8s

[INFO] 7.0s elapsed, 12/22 actions completed.
[INFO] 8.4s elapsed, 12/22 actions completed.
[INFO] 9.5s elapsed, 12/22 actions completed.
[INFO] 10.5s elapsed, 12/22 actions completed.
[INFO] 11.6s elapsed, 12/22 actions completed.
[INFO] 13.2s elapsed, 12/22 actions completed.
[INFO] 15.3s elapsed, 13/22 actions completed.
[INFO] 25.1s elapsed, 13/22 actions completed.
[INFO] 26.2s elapsed, 13/22 actions completed.
[SEVERE] freezed:freezed on lib/presentation/player_screen/bloc/player/playback_bloc.dart:
An error `FormatterException` occurred while formatting the generated source for
  `package:sound_changer/presentation/player_screen/bloc/player/playback_bloc.dart`
which was output to
  `lib/presentation/player_screen/bloc/player/playback_bloc.freezed.dart`.
This may indicate an issue in the generator, the input source code, or in the
source formatter.
Could not format because the source could not be parsed:

line 198, column 90 of .: The modifier 'required' should be before the modifier 'final'.
    ╷
198 │ @override  final String filePath;@override  final Function whenFinished;@override  final required currentSelectedTrackIndex;
    │                                                                                          ^^^^^^^^
    ╵
line 389, column 149 of .: Can't have modifier 'required' here.
    ╷
389 │ @optionalTypeArgs TResult maybeWhen<TResult extends Object?>({TResult Function()?  init,TResult Function( String filePath,  Function whenFinished,  required currentSelectedTrackIndex)?  startPlayback,TResult Function()?  pausePlayback,TResult Function()?  resumePlayback,TResult Function()?  stopPlayback,TResult Function()?  playbackEnded,required TResult orElse(),}) {
    │                                                                                                                                                     ^^^^^^^^
    ╵
line 385, column 161 of .: Can't have modifier 'required' here.
    ╷
385 │ @optionalTypeArgs TResult when<TResult extends Object?>({required TResult Function()  init,required TResult Function( String filePath,  Function whenFinished,  required currentSelectedTrackIndex)  startPlayback,required TResult Function()  pausePlayback,required TResult Function()  resumePlayback,required TResult Function()  stopPlayback,required TResult Function()  playbackEnded,}) {
    │                                                                                                                                                                 ^^^^^^^^
    ╵
line 245, column 108 of .: The modifier 'required' was already specified.
    ╷
245 │   const factory _StartPlaybackEvent({required  String filePath, required  Function whenFinished, required  required currentSelectedTrackIndex}) = _$_StartPlaybackEvent;
    │                                                                                                            ^^^^^^^^
    ╵
line 553, column 149 of .: Can't have modifier 'required' here.
    ╷
553 │ @optionalTypeArgs TResult maybeWhen<TResult extends Object?>({TResult Function()?  init,TResult Function( String filePath,  Function whenFinished,  required currentSelectedTrackIndex)?  startPlayback,TResult Function()?  pausePlayback,TResult Function()?  resumePlayback,TResult Function()?  stopPlayback,TResult Function()?  playbackEnded,required TResult orElse(),}) {
    │                                                                                                                                                     ^^^^^^^^
    ╵
line 549, column 161 of .: Can't have modifier 'required' here.
    ╷
549 │ @optionalTypeArgs TResult when<TResult extends Object?>({required TResult Function()  init,required TResult Function( String filePath,  Function whenFinished,  required currentSelectedTrackIndex)  startPlayback,required TResult Function()  pausePlayback,required TResult Function()  resumePlayback,required TResult Function()  stopPlayback,required TResult Function()  playbackEnded,}) {
    │                                                                                                                                                                 ^^^^^^^^
    ╵
line 128, column 149 of .: Can't have modifier 'required' here.
    ╷
128 │ @optionalTypeArgs TResult maybeWhen<TResult extends Object?>({TResult Function()?  init,TResult Function( String filePath,  Function whenFinished,  required currentSelectedTrackIndex)?  startPlayback,TResult Function()?  pausePlayback,TResult Function()?  resumePlayback,TResult Function()?  stopPlayback,TResult Function()?  playbackEnded,required TResult orElse(),}) {
    │                                                                                                                                                     ^^^^^^^^
    ╵
line 219, column 161 of .: Can't have modifier 'required' here.
    ╷
219 │ @optionalTypeArgs TResult when<TResult extends Object?>({required TResult Function()  init,required TResult Function( String filePath,  Function whenFinished,  required currentSelectedTrackIndex)  startPlayback,required TResult Function()  pausePlayback,required TResult Function()  resumePlayback,required TResult Function()  stopPlayback,required TResult Function()  playbackEnded,}) {
    │                                                                                                                                                                 ^^^^^^^^
    ╵
line 22, column 106 of .: The modifier 'required' was already specified.
   ╷
22 │ _StartPlaybackEvent startPlayback({required  String filePath, required  Function whenFinished, required  required currentSelectedTrackIndex}) {
   │                                                                                                          ^^^^^^^^
   ╵
line 49, column 149 of .: Can't have modifier 'required' here.
   ╷
49 │ @optionalTypeArgs TResult maybeWhen<TResult extends Object?>({TResult Function()?  init,TResult Function( String filePath,  Function whenFinished,  required currentSelectedTrackIndex)?  startPlayback,TResult Function()?  pausePlayback,TResult Function()?  resumePlayback,TResult Function()?  stopPlayback,TResult Function()?  playbackEnded,required TResult orElse(),}) => throw _privateConstructorUsedError;
   │                                                                                                                                                     ^^^^^^^^
   ╵
(9 more errors...)
[INFO] 28.2s elapsed, 28/43 actions completed.
[INFO] 29.2s elapsed, 36/49 actions completed.
[INFO] 30.4s elapsed, 38/49 actions completed.
[INFO] 31.5s elapsed, 71/76 actions completed.
[INFO] Running build completed, took 31.5s

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 98ms

[SEVERE] Failed after 31.6s
[INFO] ------------------------------------------------------------------------

[INFO] Starting Build

[INFO] Updating asset graph...
[INFO] Updating asset graph completed, took 19ms

[INFO] Running build...
[SEVERE] freezed:freezed on lib/presentation/player_screen/bloc/player/playback_bloc.dart:
An error `FormatterException` occurred while formatting the generated source for
  `package:sound_changer/presentation/player_screen/bloc/player/playback_bloc.dart`
which was output to
  `lib/presentation/player_screen/bloc/player/playback_bloc.freezed.dart`.
This may indicate an issue in the generator, the input source code, or in the
source formatter.
Could not format because the source could not be parsed:

line 198, column 90 of .: The modifier 'required' should be before the modifier 'final'.
    ╷
198 │ @override  final String filePath;@override  final Function whenFinished;@override  final required currentSelectedTrackIndex;
    │                                                                                          ^^^^^^^^
    ╵
line 389, column 149 of .: Can't have modifier 'required' here.
    ╷
389 │ @optionalTypeArgs TResult maybeWhen<TResult extends Object?>({TResult Function()?  init,TResult Function( String filePath,  Function whenFinished,  required currentSelectedTrackIndex)?  startPlayback,TResult Function()?  pausePlayback,TResult Function()?  resumePlayback,TResult Function()?  stopPlayback,TResult Function()?  playbackEnded,required TResult orElse(),}) {
    │                                                                                                                                                     ^^^^^^^^
    ╵
line 385, column 161 of .: Can't have modifier 'required' here.
    ╷
385 │ @optionalTypeArgs TResult when<TResult extends Object?>({required TResult Function()  init,required TResult Function( String filePath,  Function whenFinished,  required currentSelectedTrackIndex)  startPlayback,required TResult Function()  pausePlayback,required TResult Function()  resumePlayback,required TResult Function()  stopPlayback,required TResult Function()  playbackEnded,}) {
    │                                                                                                                                                                 ^^^^^^^^
    ╵
line 245, column 108 of .: The modifier 'required' was already specified.
    ╷
245 │   const factory _StartPlaybackEvent({required  String filePath, required  Function whenFinished, required  required currentSelectedTrackIndex}) = _$_StartPlaybackEvent;
    │                                                                                                            ^^^^^^^^
    ╵
line 553, column 149 of .: Can't have modifier 'required' here.
    ╷
553 │ @optionalTypeArgs TResult maybeWhen<TResult extends Object?>({TResult Function()?  init,TResult Function( String filePath,  Function whenFinished,  required currentSelectedTrackIndex)?  startPlayback,TResult Function()?  pausePlayback,TResult Function()?  resumePlayback,TResult Function()?  stopPlayback,TResult Function()?  playbackEnded,required TResult orElse(),}) {
    │                                                                                                                                                     ^^^^^^^^
    ╵
line 549, column 161 of .: Can't have modifier 'required' here.
    ╷
549 │ @optionalTypeArgs TResult when<TResult extends Object?>({required TResult Function()  init,required TResult Function( String filePath,  Function whenFinished,  required currentSelectedTrackIndex)  startPlayback,required TResult Function()  pausePlayback,required TResult Function()  resumePlayback,required TResult Function()  stopPlayback,required TResult Function()  playbackEnded,}) {
    │                                                                                                                                                                 ^^^^^^^^
    ╵
line 128, column 149 of .: Can't have modifier 'required' here.
    ╷
128 │ @optionalTypeArgs TResult maybeWhen<TResult extends Object?>({TResult Function()?  init,TResult Function( String filePath,  Function whenFinished,  required currentSelectedTrackIndex)?  startPlayback,TResult Function()?  pausePlayback,TResult Function()?  resumePlayback,TResult Function()?  stopPlayback,TResult Function()?  playbackEnded,required TResult orElse(),}) {
    │                                                                                                                                                     ^^^^^^^^
    ╵
line 219, column 161 of .: Can't have modifier 'required' here.
    ╷
219 │ @optionalTypeArgs TResult when<TResult extends Object?>({required TResult Function()  init,required TResult Function( String filePath,  Function whenFinished,  required currentSelectedTrackIndex)  startPlayback,required TResult Function()  pausePlayback,required TResult Function()  resumePlayback,required TResult Function()  stopPlayback,required TResult Function()  playbackEnded,}) {
    │                                                                                                                                                                 ^^^^^^^^
    ╵
line 22, column 106 of .: The modifier 'required' was already specified.
   ╷
22 │ _StartPlaybackEvent startPlayback({required  String filePath, required  Function whenFinished, required  required currentSelectedTrackIndex}) {
   │                                                                                                          ^^^^^^^^
   ╵
line 49, column 149 of .: Can't have modifier 'required' here.
   ╷
49 │ @optionalTypeArgs TResult maybeWhen<TResult extends Object?>({TResult Function()?  init,TResult Function( String filePath,  Function whenFinished,  required currentSelectedTrackIndex)?  startPlayback,TResult Function()?  pausePlayback,TResult Function()?  resumePlayback,TResult Function()?  stopPlayback,TResult Function()?  playbackEnded,required TResult orElse(),}) => throw _privateConstructorUsedError;
   │                                                                                                                                                     ^^^^^^^^
   ╵
(9 more errors...)
[INFO] Running build completed, took 968ms

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 36ms

[SEVERE] Failed after 1.0s

Haidar0096 avatar Aug 31 '21 19:08 Haidar0096

This may indicate an issue in the generator, the input source code, or in the source formatter. Could not format because the source could not be parsed:

comes from source_gen

line 198, column 90 of .: The modifier 'required' should be before the modifier 'final'.

comes from analyzer

Better error messages in this case would be awesome, but I think this improvement has to happen in the analyzer package.

SunlightBro avatar Sep 01 '21 08:09 SunlightBro

This looks like a bug in Freezed. It shouldn't cause a problem Although it's minor.

rrousselGit avatar Oct 08 '21 22:10 rrousselGit