Freezed: The getter 'stringValue' was called on null

Created on 15 Dec 2020  Â·  7Comments  Â·  Source: rrousselGit/freezed

Describe the bug
I user flutter_bloc, this morning I updated freezer to the version 0.12.6 and when I try to build, I got a similar error for every blocs:

[WARNING] freezed:freezed on lib/application/authentication/core/core_authentication_bloc.dart:
Error NoSuchMethodError: The getter 'stringValue' was called on null.
Receiver: null
Tried calling: stringValue
[WARNING] freezed:freezed on lib/application/authentication/core/core_authentication_bloc.dart:
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1      FreezedGenerator._getConstructorRedirectedName (package:freezed/src/freezed_generator.dart:501:23)
<asynchronous suspension>
#2      FreezedGenerator._parseConstructorsNeedsGeneration (package:freezed/src/freezed_generator.dart:219:17)
#3      FreezedGenerator.parseElement (package:freezed/src/freezed_generator.dart:76:47)
#4      ParserGenerator.generate (package:freezed/src/parse_generator.dart:36:28)
<asynchronous suspension>
#5      _generate (package:source_gen/src/builder.dart:323:33)
<asynchronous suspension>
#6      _Builder._generateForLibrary (package:source_gen/src/builder.dart:84:15)
#7      _Builder.build (package:source_gen/src/builder.dart:76:11)
<asynchronous suspension>
#8      runBuilder.buildForInput (package:build/src/generate/run_builder.dart:53:21)
#9      MappedListIterable.elementAt (dart:_internal/iterable.dart:417:31)
#10     ListIterator.moveNext (dart:_internal/iterable.dart:343:26)
#11     Future.wait (dart:async/future.dart:406:26)
#12     runBuilder.<anonymous closure> (package:build/src/generate/run_builder.dart:59:36)
#13     _rootRun (dart:async/zone.dart:1190:13)
#14     _CustomZone.run (dart:async/zone.dart:1093:19)
#15     _runZoned (dart:async/zone.dart:1630:10)
#16     runZonedGuarded (dart:async/zone.dart:1618:12)
#17     scopeLogAsync (package:build/src/builder/logging.dart:26:3)
#18     runBuilder (package:build/src/generate/run_builder.dart:59:9)
#19     _SingleBuild._runForInput.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:build_runner_core/src/generate/build_impl.dart:527:19)
#20     _NoOpBuilderActionTracker.trackStage (package:build_runner_core/src/generate/performance_tracker.dart:302:15)
#21     _SingleBuild._runForInput.<anonymous closure>.<anonymous closure> (package:build_runner_core/src/generate/build_impl.dart:525:23)
<asynchronous suspension>
#22     _SingleBuild._runForInput.<anonymous closure>.<anonymous closure> (package:build_runner_core/src/generate/build_impl.dart)
#23     NoOpTimeTracker.track (package:timing/src/timing.dart:222:44)
#24     _SingleBuild._runForInput.<anonymous closure> (package:build_runner_core/src/generate/build_impl.dart:475:22)
#25     Pool.withResource (package:pool/pool.dart:127:28)
<asynchronous suspension>
#26     _SingleBuild._runForInput (package:build_runner_core/src/generate/build_impl.dart:471:17)
#27     _SingleBuild._runBuilder.<anonymous closure> (package:build_runner_core/src/generate/build_impl.dart:409:38)
#28     MappedIterator.moveNext (dart:_internal/iterable.dart:392:20)
#29     Future.wait (dart:async/future.dart:406:26)
#30     _SingleBuild._runBuilder (package:build_runner_core/src/generate/build_impl.dart:408:36)
#31     _SingleBuild._runPhases.<anonymous closure>.<anonymous closure> (package:build_runner_core/src/generate/build_impl.dart:347:20)
<asynchronous suspension>
#32     _SingleBuild._runPhases.<anonymous closure>.<anonymous closure> (package:build_runner_core/src/generate/build_impl.dart)
#33     _NoOpBuildPerformanceTracker.trackBuildPhase (package:build_runner_core/src/generate/performance_tracker.dart:184:15)
#34     _SingleBuild._runPhases.<anonymous closure> (package:build_runner_core/src/generate/build_impl.dart:343:47)
<asynchronous suspension>
#35     _SingleBuild._runPhases.<anonymous closure> (package:build_runner_core/src/generate/build_impl.dart)
#36     NoOpTimeTracker.track (package:timing/src/timing.dart:222:44)
#37     _SingleBuild._runPhases (package:build_runner_core/src/generate/build_impl.dart:337:32)
#38     logTimedAsync (package:build_runner_core/src/logging/logging.dart:25:30)
#39     _SingleBuild._safeBuild.<anonymous closure> (package:build_runner_core/src/generate/build_impl.dart:294:26)
#40     _rootRun (dart:async/zone.dart:1190:13)
#41     _CustomZone.run (dart:async/zone.dart:1093:19)
#42     _runZoned (dart:async/zone.dart:1630:10)
#43     runZonedGuarded (dart:async/zone.dart:1618:12)
#44     _SingleBuild._safeBuild (package:build_runner_core/src/generate/build_impl.dart:289:5)
#45     _SingleBuild.run (package:build_runner_core/src/generate/build_impl.dart:236:24)
#46     BuildImpl.run (package:build_runner_core/src/generate/build_impl.dart:97:56)
#47     BuildRunner.run (package:build_runner_core/src/generate/build_runner.dart:25:14)
#48     build (package:build_runner/src/generate/build.dart:107:21)
<asynchronous suspension>
#49     BuildCommand._run (package:build_runner/src/entrypoint/build.dart:35:24)
#50     BuildCommand.run.<anonymous closure> (package:build_runner/src/entrypoint/build.dart:31:15)
#51     _rootRun (dart:async/zone.dart:1190:13)
#52     _CustomZone.run (dart:async/zone.dart:1093:19)
#53     _runZoned (dart:async/zone.dart:1630:10)
#54     runZoned (dart:async/zone.dart:1550:10)
#55     withEnabledExperiments (package:build/src/experiments.dart:18:5)
#56     BuildCommand.run (package:build_runner/src/entrypoint/build.dart:30:12)
#57     CommandRunner.runCommand (package:args/command_runner.dart:197:27)
#58     CommandRunner.run.<anonymous closure> (package:args/command_runner.dart:112:25)
#59     new Future.sync (dart:async/future.dart:223:31)
#60     CommandRunner.run (package:args/command_runner.dart:112:14)
#61     run (package:build_runner/src/entrypoint/run.dart:25:31)
<asynchronous suspension>
#62     main (file:///D:/Programming/codename-wine/wine/.dart_tool/build/entrypoint/build.dart:41:22)
#63     _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:32)
#64     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)

[SEVERE] freezed:freezed on lib/application/authentication/core/core_authentication_bloc.dart:

NoSuchMethodError: The getter 'stringValue' was called on null.
Receiver: null
Tried calling: stringValue

To Reproduce
Try creating a bloc as such:

bloc

import 'dart:async';

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

part 'auth_bloc.freezed.dart';
part 'auth_event.dart';
part 'auth_state.dart';

/// @nodoc
@injectable
class AuthBloc extends Bloc<AuthEvent, AuthState> {
  /// @nodoc
  AuthBloc() : super(AuthState.initial());

  @override
  Stream<AuthState> mapEventToState(
    AuthEvent event,
  ) async* {
    yield* event.map(
      appLaunchedEVT: (event) async* {
        yield state.copyWith(isAnonymous: true);
      },
      userStatusChangedEVT: (event) async* {
        yield state.copyWith(isAnonymous: false);
      },
    );
  }
}

event

part of 'auth_bloc.dart';

/// @nodoc
@freezed
abstract class AuthEvent with _$AuthEvent {
  /// @nodoc
  const factory AuthEvent.appLaunchedEVT() = AppLaunchedEVT;

  /// @nodoc
  const factory AuthEvent.userStatusChangedEVT() =
      UserStatusChangedEVT;
}

state

part of 'auth_bloc.dart';

/// @nodoc
@freezed
abstract class AuthState with _$AuthState {
  /// @nodoc
  const factory AuthState({
    @required bool isAnonymous,
  }) = _AuthState;

  /// @nodoc
  factory AuthState.initial() =>
      const AuthState(isAnonymous: false);
}

Expected behavior
A file auth_bloc.freezed.dart should be generated

Environnement

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 1.22.4, on Microsoft Windows [version 10.0.19041.685], locale fr-FR)

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.0-rc1)
[!] Android Studio (version 4.1.0)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
[✓] VS Code (version 1.52.0)
[!] Connected device
    ! No devices available

! Doctor found issues in 2 categories.

Disregard Android Studio, I use VSCode for Flutter dev.

bug needs triage

Most helpful comment

Thanks SandroMaglione !

Turns out it wasn't a versioning problem for me. It works when i use freezed 0.12.6. The problem was triple slashes in my code. For example, this fails and throws the stringValue error

@freezed
abstract class AuthState with _$AuthState {
  /// This works in conjunction with `showLoadingDialog` to let the
  /// user know that the app is loading

  /// A failure when an authentication function fails.
  /// This works in conjunction with `showErrorFlushbar` to let the user
  /// know of the failure
  const factory AuthState.loggedOut({
    @Default(false) bool loading,
    AuthFailure failure,
    Success success,
  }) = LoggedOut;

  /// Add any fields here to store the user's info once they are logged in
  const factory AuthState.loggedIn({
    String uid,
    String username,
    String email,
  }) = LoggedIn;
}

But when I change all the triple slashes to double slashes, everything works fine, even with freezed 0.12.6

All 7 comments

Same issue here for this small class:

import 'package:freezed_annotation/freezed_annotation.dart';

part 'tracking_screen.freezed.dart';

@freezed
abstract class TrackingScreen with _$TrackingScreen {
  /// View Events
  const factory TrackingScreen.view(String title) = View;

  const factory TrackingScreen.controller(String title) = Controller;

  const factory TrackingScreen.sheet(String title) = Sheet;

  const factory TrackingScreen.alert(String title) = Alert;
}

Same here.

It seems to be a bug in freezed 0.12.6 since I tried to downgrade to freezed 0.12.5 and it works.

Hey @SandroMaglione,

I tried downgrading to 0.12.5 and even to 0.12.1 but no luck. Could I ask you what version of build_runner you are using?

I'm on Dart 2.10.4 and Flutter 1.22.5

@theweiweiway
build_runner: 1.10.3
analyzer: 0.40.6
flutter: 1.22.4
dart: 2.10.4

freezed locked at version 0.12.5

freezed: 0.12.5

Thanks SandroMaglione !

Turns out it wasn't a versioning problem for me. It works when i use freezed 0.12.6. The problem was triple slashes in my code. For example, this fails and throws the stringValue error

@freezed
abstract class AuthState with _$AuthState {
  /// This works in conjunction with `showLoadingDialog` to let the
  /// user know that the app is loading

  /// A failure when an authentication function fails.
  /// This works in conjunction with `showErrorFlushbar` to let the user
  /// know of the failure
  const factory AuthState.loggedOut({
    @Default(false) bool loading,
    AuthFailure failure,
    Success success,
  }) = LoggedOut;

  /// Add any fields here to store the user's info once they are logged in
  const factory AuthState.loggedIn({
    String uid,
    String username,
    String email,
  }) = LoggedIn;
}

But when I change all the triple slashes to double slashes, everything works fine, even with freezed 0.12.6

So this issue is a conflict with Dart documentation's format (IIRC triple slashed are for documentation), that's quite problematic.

This should be fixed as part of 0.12.7

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jonasbark picture jonasbark  Â·  3Comments

orestesgaolin picture orestesgaolin  Â·  6Comments

pblinux picture pblinux  Â·  4Comments

matiasosuna picture matiasosuna  Â·  4Comments

ndinhbang picture ndinhbang  Â·  5Comments