I have a @freezed class defined like below and I need to be able to use its artistId
in order to make an API call and get an Artist
object. Since that object will be used across multiple screens and filtering functions, I don't want to make requests every single time.
Since the freezed library doesn't allow mutable state in annotated classes, what would be the best workaround?
import 'package:freezed_annotation/freezed_annotation.dart';
import '../../abstract/repository/artist/artist_repository.dart';
import '../artist/artist.dart';
import '../fair/fair.dart';
import '../provenance/provenance.dart';
part 'photo.g.dart';
part 'photo.freezed.dart';
enum OwnershipType { user, artist, collector }
@freezed
class Photo with _$Photo {
factory Photo({
required int id,
required int artistId,
required String title,
required String year,
double? height,
double? length,
double? depth,
required String photo,
required String approved,
required String creationDate,
@JsonKey(name: 'show') Fair? fair,
// required List<Fair> exhibited,
required String description,
required List<Provenance> provenance,
required String submittedDate,
String? submitterName,
bool? pendingUpdate,
String? dimensionUnits,
@Default(<String>[]) List<String> galleryNames,
String? thumb,
}) = _Photo;
factory Photo.fromJson(Map<String, dynamic> json) => _$PhotoFromJson(json);
Photo._();
String get firebasePhoto {
return 'https://storage.googleapis.com/download/storage/v1/b/blob-venture.appspot.com/o/${photo.replaceAll('/', '%2F')}?alt=media';
}
//following is not allowed sadly
// Artist? artist;
// Future<void> fetchArtist(ArtistRepository artistRepository) async {
// artist = await artistRepository.getArtist(id: artistId);
// }
}
I've thought about making another model that will hold the Photo object along with its corresponding Artist object after fetching it, but if there's a better way to go about it, I'd love to know.
Maybe there's another library that allows for such cases? This library was selected by the previous dev of the project but it's not been a great tool for me so far.