Freezed - the getter 'x' isn't defined for x

1.8k views Asked by At

I'm trying to get started with Freezed, and I define this class below, and the rest of my app doesn't seem to pick up that it has an id property:

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

part 'user.freezed.dart';

@freezed
abstract class VpUser implements _$VpUser {
  factory VpUser({String id, User firebaseUser}) = _VpUser;
  const factory VpUser.empty() = _Empty;
  const VpUser._();
}

I use the class in my authentication service:

class AuthenticationService extends VpService {
  final FirebaseAuth _auth = FirebaseAuth.instance;
  final Rx<User> _firebaseUser = Rx<User>();
  VpUser get user =>
      VpUser(id: _firebaseUser.value?.uid, firebaseUser: _firebaseUser.value);

then I call the user attached to the AuthService like so:

if (_authenticationService.user.id != null) {

And I have the error:

Error: The getter 'id' isn't defined for the class 'VpUser'.

  • 'VpUser' is from 'package:vepo/domain/auth/user.dart' ('lib/domain/auth/user.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named 'id'. if (_authenticationService.user.id != null) {

How do I declare the id in VpUser so other classes can access it?

1

There are 1 answers

0
BeniaminoBaggins On

It must have been my VpUser.empty implementation, not declaring the id field.

I think this is how to do an empty value without making the rest of the app not see the property:

const factory VpUser.empty(
  {@nullable @Default(null) String id,
  @nullable @Default(null) User firebaseUser}) = _Empty;