Flutter freezed package - Error: Type '$UserProfileCopyWith' not found in Xcode build

90 views Asked by At

Issue

Please help me . The issue I'm struggling with occurs as I add a Freezed class UserProfile included in a local package user_profile_repository

to the Freezed class HomeUserProfileState representing loading state of fetching user data for the home screen

and being part of HomeState.

As running flutter run xcode build fails:

Launching lib/main.dart on iPad mini (6th generation) in debug mode...
main.dart:1
Xcode build done.                                            9.2s
Failed to build iOS app
Error (Xcode): lib/home/cubit/home_cubit.freezed.dart:1147:3: Error: Type '$UserProfileCopyWith' not found.
home_cubit.freezed.dart:1147
Could not build the application for the simulator.
Error launching application on iPad mini (6th generation).
Exited

The packages versions I'm using are resolved by flutter pub get to freezed 2.4.5, freezed_annotation 2.4.1 and build_runner 2.4.6.

Below I included hopefully all information necessary for your support. Please tell me if you're missing something.

Local package 'user_profile_repository'

part 'user_profile.freezed.dart';

@freezed
class UserProfile with _$UserProfile {
  factory UserProfile({
    required String id,
    required String username,
    required String email,
    required String firstName,
  }) = _UserProfile;

  factory UserProfile.fromUserAttributes(
    List<AuthUserAttribute> userAttributes,
  ) =>
      UserProfileConverter().convert(userAttributes);
}

packages/user_profile_repository/lib/src/models/user_profile.dart

UserProfile is exported in the package.

export 'src/models/user_profile.dart';
export 'src/user_profile_repository.dart';

packages/user_profile_repository/lib/user_profile_repository.dart

HomeUserProfileState and HomeState classes

Here is how UserProfile is included into HomeUserProfileState.loaded:

part of 'home_cubit.dart';

@freezed
class HomeState with _$HomeState {
  const factory HomeState({
    @Default(HomeMoodsState.initial()) HomeMoodsState moodsState,
    @Default(HomeUserProfileState.initial())
    HomeUserProfileState userProfileState,
  }) = _HomeState;
}

@freezed
class HomeUserProfileState with _$HomeUserProfileState {
  const factory HomeUserProfileState.initial() = HomeUserProfileInitialState;
  const factory HomeUserProfileState.loading() = HomeUserProfileLoadingState;
  const factory HomeUserProfileState.loaded({
    required user_profile_repository.UserProfile userProfile,
  }) = HomeUserProfileSuccessState;
  const factory HomeUserProfileState.error() = HomeUserProfileErrorState;
}

// ...

lib/home/cubit/home_state.dart

As I delete the parameter userProfile in HomeUserProfileState.loaded and run build_runner again xcode build runs without issues.

HomeCubit

The class HomeCubit emits the state using HomeState.

import 'dart:async';

import 'package:amplify_flutter/amplify_flutter.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:mood_repository/mood_repository.dart';
import 'package:user_profile_repository/user_profile_repository.dart'
    as user_profile_repository;

part 'home_cubit.freezed.dart';
part 'home_state.dart';

class HomeCubit extends Cubit<HomeState> {
  HomeCubit({
    required MoodRepository moodRepository,
    required DataStoreCategory dataStore,
    required user_profile_repository.UserProfileRepository
        userProfileRepository,
  })  : _moodRepository = moodRepository,
        _userProfileRepository = userProfileRepository,
        _dataStore = dataStore,
        super(const HomeState());

  final MoodRepository _moodRepository;

  final user_profile_repository.UserProfileRepository _userProfileRepository;

  final DataStoreCategory _dataStore;

  late final StreamSubscription<SubscriptionEvent<MoodEntry>>? moodEntryStream;

  Future<void> init() async {
    await loadUserProfile();
    await loadMoods();
    listenMoodEntryChanges();
  }

  // ...
  
  Future<void> loadUserProfile() async {
    try {
      emit(
        state.copyWith(
          userProfileState: const HomeUserProfileState.loading(),
        ),
      );

      final userProfile = await _userProfileRepository.fetchUserProfile();

      emit(
        state.copyWith(
          userProfileState: HomeUserProfileState.loaded(
            userProfile: userProfile,
          ),
        ),
      );
    } catch (e) {
      emit(
        state.copyWith(
          userProfileState: const HomeUserProfileState.error(),
        ),
      );
    }
  }
}

As I delete the parameter userProfile in HomeUserProfileState.loaded and run build_runner again xcode build runs without issues.

0

There are 0 answers