0

I have code like this:

import 'package:freezed_annotation/freezed_annotation.dart';

part 'token_state.freezed.dart';

@freezed
class TokenState with _$TokenState {
  const factory TokenState({
    required String token,
  }) = _TokenState;
}

What will change if I add one line to this code?

import 'package:freezed_annotation/freezed_annotation.dart';

part 'token_state.freezed.dart';

@freezed
class TokenState with _$TokenState {
  const factory TokenState({
    required String token,
  }) = _TokenState;
  TokenState._(); // add in
}

What does adding the code TokenState._(); change?

sub
  • 117
  • 1
  • 6

1 Answers1

1

It creates a library-private constructor, ensuring that there's no default constructor. This is important because you don't want anyone creating an instance of the class or its subclasses except through the use of the factory constructor there.

Randal Schwartz
  • 39,428
  • 4
  • 43
  • 70