getLostData method

  1. @override
Future<LostDataResponse> getLostData()

Retrieves any previously picked files, that were lost due to the MainActivity being destroyed. (Android only)

Image or video can be lost if the MainActivity is destroyed. And there is no guarantee that the MainActivity is always alive. Call this method to retrieve the lost data and process the data according to your APP's business logic.

Returns a LostDataResponse object if successfully retrieved the lost data. The LostDataResponse object can represent either a successful image/video selection, or a failure.

Calling this on a non-Android platform will throw UnimplementedError exception.

See also:

  • LostDataResponse, for what's included in the response.
  • Android Activity Lifecycle, for more information on MainActivity destruction.

Implementation

@override
Future<LostDataResponse> getLostData() async {
  final CacheRetrievalResult? result = await _hostApi.retrieveLostResults();

  if (result == null) {
    return LostDataResponse.empty();
  }

  // There must either be data or an error if the response wasn't null.
  assert(result.paths.isEmpty != (result.error == null));

  final CacheRetrievalError? error = result.error;
  final PlatformException? exception = error == null
      ? null
      : PlatformException(code: error.code, message: error.message);

  // Entries are guaranteed not to be null, even though that's not currently
  // expressible in Pigeon.
  final List<XFile> pickedFileList =
      result.paths.map((String? path) => XFile(path!)).toList();

  return LostDataResponse(
    file: pickedFileList.isEmpty ? null : pickedFileList.last,
    exception: exception,
    type: _retrieveTypeForCacheType(result.type),
    files: pickedFileList,
  );
}