I'm going to create Photo entity in firebase store and I need to get two fields like current user's email and Photo url but my BLoC doesn't update state with asynchronously received data. Here is my code
saved: (e) async* {
Either<PhotoFailure, Unit> failureOrSuccess;
final userOption = await _authFacade.getSignedInUser();
final user = userOption.fold(() => null, (user) => user);
print(user.emailAddress.getOrCrash());
yield state.copyWith(
photo:
state.photo.copyWith(author: user.emailAddress.getOrCrash()));
print(state.photo.author);
yield state.copyWith(
photo: state.photo.copyWith(
uploadDate: DateFormat("dd-MM-yyyy").format(DateTime.now())));
print(state.photo.uploadDate);
FirebaseStorage _storage = FirebaseStorage.instance;
Reference _rootReference = _storage.ref().child('photos');
UploadTask task = _rootReference.putFile(state.photoFile);
String downloadUrl = await (await task).ref.getDownloadURL();
print(downloadUrl);
yield state.copyWith(
photo: state.photo.copyWith(url: downloadUrl),
);
print(state.photo.url);
yield state.copyWith(
isSaving: true,
saveFailureOrSuccessOption: none(),
);
if (state.photo.failureOption.isNone()) {
state.isEditing
? await _photoRepository.update(state.photo)
: await _photoRepository.create(state.photo);
}
yield state.copyWith(
isSaving: false,
showErrorMessages: AutovalidateMode.always,
saveFailureOrSuccessOption: optionOf(failureOrSuccess),
);
},
);
}
As you can see I've decided to print all results and Console logs this
I/flutter (19212): test@gmail.com
I/flutter (19212):
I/flutter (19212): 2021-01-07 16:47:23.279989
D/UploadTask(19212): Increasing chunk size to 524288
I/flutter (19212): https://firebasestorage.googleapis.com/v0/b/simplefirebasegalley.appspot.com/o/photos?alt=media&token=73b3537f-d36c-49a8-b080-5cd27837c50e
I/flutter (19212):
So i'm getting correct data but i can't update state with only asynchronous data because uploadDate works correct.
My state code
@freezed
abstract class PhotoFormState with _$PhotoFormState {
const factory PhotoFormState({
@required Photo photo,
@nullable @required File photoFile,
@required AutovalidateMode showErrorMessages,
@required bool isEditing,
@required bool isSaving,
@required Option<Either<PhotoFailure, Unit>> saveFailureOrSuccessOption,
}) = _PhotoFormState;
factory PhotoFormState.initial() => PhotoFormState(
photo: Photo.empty(),
photoFile: null,
showErrorMessages: AutovalidateMode.disabled,
isEditing: false,
isSaving: false,
saveFailureOrSuccessOption: none(),
);
}
And here is my Photo entity class
@freezed
abstract class Photo with _$Photo {
const Photo._();
const factory Photo({
@required String url,
@required String type,
@required int watchCount,
@required UniqueId id,
@required PhotoName name,
@required PhotoDescription description,
@required TagList<Tag> tagList,
@required String author,
@required String uploadDate,
@required FieldValue serverTimeStamp,
}) = _Photo;
factory Photo.empty() => Photo(
url: '',
type: 'new',
id: UniqueId(),
name: PhotoName(''),
description: PhotoDescription(''),
tagList: TagList(new List()),
watchCount: 0,
author: '',
serverTimeStamp: FieldValue.serverTimestamp(),
uploadDate: DateTime.now().toString(),
);
Option<ValueFailure<dynamic>> get failureOption {
return name.failureOrUnit
.andThen(description.failureOrUnit)
.andThen(tagList.failureOrUnit)
.fold((f) => some(f), (r) => none());
}
}