'The asset does not exist or has empty data' but the asset exists and has data
Asked Answered
J

3

9

I put in pubspec.yaml and I ran flutter pub get. I can even view the files on the VS code in built file explorer but it still gives the same error.

class OnloginScreen extends StatefulWidget {
  const OnloginScreen({super.key});

  @override
  State<OnloginScreen> createState() => _OnloginScreenState();
}

class _OnloginScreenState extends State<OnloginScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(children: [
        RiveAnimation.asset('assets\RiveAssets\shapes.riv'),
      ]),
    );
  }
}

The error:

E/flutter ( 4055): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Unable to load asset: "assetsRiveAssetsshapes.riv".
E/flutter ( 4055): The asset does not exist or has empty data.
E/flutter ( 4055): #0      PlatformAssetBundle.load.<anonymous closure> (package:flutter/src/services/asset_bundle.dart:254:9)
E/flutter ( 4055): <asynchronous suspension>
E/flutter ( 4055): #1      RiveFile.asset (package:rive/src/rive_file.dart:263:19)       
E/flutter ( 4055): <asynchronous suspension>
E/flutter ( 4055): #2      RiveAnimationState._configure (package:rive/src/widgets/rive_animation.dart:176:11)
E/flutter ( 4055): <asynchronous suspension>
E/flutter ( 4055):

pubspec.yaml

name: nee_app
description: A new Flutter project.

# The following line prevents the package from being accidentally published to
# pub.dev using `flutter pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number is used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 1.0.0+1

environment:
  sdk: '>=2.18.6 <3.0.0'

# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
# consider running `flutter pub upgrade --major-versions`. Alternatively,
# dependencies can be manually updated by changing the version numbers below to
# the latest version available on pub.dev. To see which dependencies have newer
# versions available, run `flutter pub outdated`.
dependencies:
  flutter:
    sdk: flutter


  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.2
  intl: ^0.18.0
  smooth_page_indicator: ^1.0.0+2
  badges: ^2.0.3
  flutter_neumorphic: ^3.2.0
  lottie: ^2.1.0
  injectable: ^2.1.0
  provider: ^6.0.5
  timezone: ^0.9.1
  flutter_local_notifications: ^13.0.0
  rive: ^0.10.2
  flutter_svg: ^2.0.2


dev_dependencies:
  flutter_test:
    sdk: flutter

  # The "flutter_lints" package below contains a set of recommended lints to
  # encourage good coding practices. The lint set provided by the package is
  # activated in the `analysis_options.yaml` file located at the root of your
  # package. See that file for information about deactivating specific lint
  # rules and activating additional ones.
  flutter_lints: ^2.0.0

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter packages.
flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  assets:
    - assets/avaters/
    - assets/Backgrounds/
    - assets/icons/
    - assets/RiveAssets/

  # An image asset can refer to one or more resolution-specific "variants", see
  # https://flutter.dev/assets-and-images/#resolution-aware

  # For details regarding adding assets from package dependencies, see
  # https://flutter.dev/assets-and-images/#from-packages

  # To add custom fonts to your application, add a fonts section here,
  # in this "flutter" section. Each entry in this list should have a
  # "family" key with the font family name, and a "fonts" key with a
  # list giving the asset and other descriptors for the font. For
  # example:
  fonts:
    - family: Montserrat
      fonts:
        - asset: assets/fonts/Montserrat-Italic-VariableFont_wght.ttf
        - asset: assets/fonts/Montserrat-VariableFont_wght.ttf
    - family: Intel
      fonts:
        - asset: assets/fonts/Inter-Regular.ttf
        - asset: assets/fonts/Inter-SemiBold.ttf
          weight: 600
    - family: Poppins
      fonts:
        - asset: assets/fonts/Poppins-Bold.ttf
          weight: 700

Its the same for all other assets whether it's a Rive asset or just an image I try to use on OnloginScreen() I have no idea why this is happening. flutter doctor says everything is normal.

Johanajohanan answered 6/3, 2023 at 13:1 Comment(0)
H
5

It's a path reading error. In line:

RiveAnimation.asset('assets\RiveAssets\shapes.riv')

You need to change your backslashes \ for slashes /, like this:

RiveAnimation.asset('assets/RiveAssets/shapes.riv')
Harlin answered 6/3, 2023 at 13:27 Comment(0)
V
2

Just add relative path to the asset in pubspec.yaml where your images is located. like this -

Don't do -

assets:
  - assets/images/

Do -

assets:
  - assets/images/onbaording/
  - assets/images/home/ **(my all images is here)**
Vilma answered 16/7, 2023 at 18:43 Comment(0)
C
1

In my case i was trying to get the get the asset from another package that belongs to the workspace, i was sure the pub was ok but still getting that error the solution its to specify the package

SvgPicture.asset(
        'assets/svg/apple-logo.svg',
        height: size,
        package: 'panorama_design_system', //HEREEE
      ),
Coven answered 23/2 at 0:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.