FileNotFoundError with flutter_dotenv - flutter

I installed flutter_dotenv with this command:
flutter pub add flutter_dotenv
My is pubspec.yaml is like:
dependencies:
flutter:
flutter_dotenv: ^5.0.0
flutter:
assets:
- .env
Then I put .env file in the project root (I will use .env.dev. .env.prd later so you see them in the screen shot):
I run project from VSCode then get FileNotFoundError:
I checked the .env file location hundred of times, tried to change the file name, change the location to /lib etc - still no luck. Any idea?

I found what was wrong. "assets:" must be under "flutter:", so tab is necessary before "assets:". That's it!
flutter:
assets:
- .env

Related

how can I add assets in flutter pubspec.yaml

I tried to just add (assets/ ) but that didn't solve the problem
The assets to specify in the pubspec should be inside the flutter section.
flutter:
assets:
- assets/my_icon.png
- assets/background.png
also example with directories:
flutter:
assets:
- directory/
- directory/subdirectory/
Take a fast read to the official docs: https://docs.flutter.dev/development/ui/assets-and-images
Follow below steps
Create a new Folder name "assets" parallel to Lib folder
Create a folder name "images" in it.
Define a assets like a below in a "pubspec.yaml"
assets:
assets/images/
Use it like a below
Image.asset("assets/images/my-profile.png"),

The file "assets/background.png" set as the parameter "image" was not found. pub finished with exit code 1

I am trying to create a splash screen using flutter_native_splash 1.3.1 package. I created a folder called 'assets' in the lib folder and added that image to that folder. But I am getting the error mentioned in the question when I ran 'flutter pub run flutter_native_splash:create' in the command line.
I added the following lines in pubspec.yaml as suggested by the package documentation.
flutter_native_splash:
color: "#000000"
image: assets/background.png
Can someone please explain what I might be missing?
Do the following steps
Add flutter_native_splash: ^1.3.1 in pubspec.yaml
Create flutter_native_splash.yaml in root of project at same level as pubspec.yaml
Add below content in the above file
flutter_native_splash:
color: "#000000"
image: assets/background.png
Run this command in terminal
flutter pub run flutter_native_splash:create
With above steps, splash screen will load properly but the error you mentioned might come. To remove that error enable below section in pubspec.yaml
assets:
- assets/background.png
6.Make sure you have assets directory in the root of project and background.png is present inside it.
7.Clean and build. it should work without any issues
Continuing the Ehtesham Siddiquie answer.
I think the assets folder should be at the root of the project (at the same level of pubspec.yaml and flutter_native_splash.yaml). but u created it in the lib folder.

flutter unable to load image assets

this how I called image in my app
Image.asset('assets/user.jpg')
this is my pubspec.yaml
assets:
-assets/user.jpg
and I get this error
Exception has occurred. FlutterError (Unable to load asset: assets/user.jpg)
You must consider indentation for assets in pubspec.yaml
assets:
- assets/user.jpg
Your indentation is not correct. It should be like this:
assets:
- assets/user.jpg
Make sure user.jpg is placed in assets folder in your project's
main directory (not inside lib folder)
After doing this, make sure you rebuild your app to see these changes.

Unable to run the flutter application

I get this error when I try running the application.
Error: unable to find directory entry in pubspec.yaml: /Users/yoshithKotla/Desktop/Freewheel_application/assets/images/
# To add assets to your application, add an assets section, like this:
assets:
- assets/images/logo.png
comment out the assets: & make sure to get the path correctly.
You should comment below part from pubspec.yaml if you haven't any directory assets/images in your project.
To add assets to your application, add an assets section, like this:
assets:
- images/a_dot_burr.jpeg
If you have assets and images folder than you should change your pubspec.yaml file like this,
To add assets to your application, add an assets section, like this:
assets:
- assets/images

flutter web app deployed on github.pages cannot access SOME assets

[UPDATED checkout the issue with steps on github ]
running my flutter web app locally
flutter run -d chrome --dart-define=FLUTTER_WEB_USE_SKIA=true --release
works as intended (video), but building it and deploying it to github pages (here)
flutter_master build web --dart-define=FLUTTER_WEB_USE_SKIA=true --release
doesn't access some asset, but successfully access others.
I've tried these solutions (one, two)
'about.json' works as expected locally but fails to load when deployed
while 'assets/about.json' doesn't work in either cases
the code in use can be simplified as
rootBundle.loadString('about.json');
I double-checked pubspec.yaml
flutter:
uses-material-design: true
assets:
- background_portrait.jpg
- background_landscape.jpg
- yf_icon_black.png
- yf_logo.png
- about.json
- apps.json
- news.json
- opensource.json
and the assets in the build folder
everything checks out, but the issue still persists
in these logs you can see that those files are present
What worked for me was to eliminate the assets folder completely. I created a folder for each of my asset types in the root dir (same level as lib) and referenced them as directories in pubspec.yaml:
assets:
- json/
- avatars/
Then when loading them I used a relative path as:
await rootBundle.loadString('json/structure.json');
Flutter creates an assets folder during build and copies all my asset directories into it. This way it worked for me to load the assets both in debug and in release mode on GitLab Pages.
EDIT: I include the gitlab.ci.yml file I use for gitlab pages build pipeline
image: registry.gitlab.com/famedly/containers/flutter-dockerimages:beta
pages:
script:
- flutter clean
- flutter config --enable-web
- flutter pub get
- flutter build web --release
- ls build/web
- cp -r build/web public
- ls public
artifacts:
paths:
- public
only:
- master
The ls commands you do not need these were just for logging the output during development of the script. I left them there because they do no harm and could come handy sometime.
The official flutter documentation explains that the assets should be added relative to the path of pubspec.yaml
For your example of pubspec.yaml, you should either move the asset files in the root of you project folder or if they are located in an assets sub folder add the name of that sub folder in pubspec.yaml
E.g. if your files are located under project_path/assets/, the assets section of your pubspec.yaml should look something like:
flutter:
uses-material-design: true
assets:
- assets/background_portrait.jpg
- assets/background_landscape.jpg
- assets/yf_icon_black.png
- assets/yf_logo.png
- assets/about.json
- assets/apps.json
- assets/news.json
- assets/opensource.json
In your dart code, assets should be accessed by their specified key, for the above example use 'assets/about.json' and not 'about.json'
Its an old thread - just in case someone stumbles upon...you have to ensure that a valid certificate is in place. Otherwise the service worker will not start.