file method

Future<File?> file()

Implementation

Future<rive.File?> file() async {
  if (_providedFile != null) {
    _loadedFile = _providedFile;
    return _providedFile;
  }

  if (_loadCompleter != null) {
    return _loadCompleter!.future;
  }

  _loadCompleter = Completer<rive.File>();

  try {
    rive.File? file;

    if (_asset != null) {
      try {
        file = await rive.File.asset(_asset!, riveFactory: riveFactory);
      } catch (e) {
        // Handle the case where the asset doesn't exist or has empty data
        throw RiveFileLoaderException(
            'Failed to load Rive file from asset: $_asset. Error: $e');
      }
      if (file == null) {
        throw RiveFileLoaderException(
            'Failed to decode Rive file from asset: $_asset');
      }
    } else if (_url != null) {
      try {
        file = await rive.File.url(_url!, riveFactory: riveFactory);
      } catch (e) {
        // Handle the case where the URL fails to load
        throw RiveFileLoaderException(
            'Failed to load Rive file from URL: $_url. Error: $e');
      }
      if (file == null) {
        throw RiveFileLoaderException(
            'Failed to decode Rive file from URL: $_url');
      }
    } else {
      throw RiveFileLoaderException(
          'No asset, URL, or file provided to RiveFileLoader');
    }

    _loadedFile = file;
    _loadCompleter!.complete(file);
    return file;
  } on RiveFileLoaderException catch (e) {
    _loadCompleter!.completeError(e);
    rethrow;
  } on Exception catch (e) {
    final riveException =
        RiveFileLoaderException('Unexpected error loading Rive file: $e');
    _loadCompleter!.completeError(riveException);
    throw riveException;
  }
}