Stelios Papamichail
Stelios Papamichail

Reputation: 1270

Combine freezed lib with model state

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.

Upvotes: 0

Views: 501

Answers (1)

Robert Sandberg
Robert Sandberg

Reputation: 8597

  1. You could make a service that handle the API requests.
  2. You could have a state management solution (e.g. flutter_bloc) that handles calling the service. The state management solution could be constructed to provide all the information down a specific widget tree, or the entire app.
  3. Convert the result from your API to a "freezed-model-classes".

freezed is amazing and imo is a great choice by your colleagues. Just make sure to realize that package shouldn't handle everything in your app. In other words, if you are making API calls from your model classes, you could improve the architecture of your app.

To expand further, you could put Artist? as a part of your Photo class by putting it with the rest of your parameters. And you can use the copyWith method to "add" (actually copy with added data as name suggests) new information to an existing Photo object.

Upvotes: 1

Related Questions