can not load dotEnv file from assets in released app - flutter

I have created a dotEnv file in root of project and added that file to pubspec.yaml file like this:
assets:
- dotEnv.develop
then I reference it in main.dart file this way:
void main() async{
await dotenv.load(fileName: "dotEnv.develop");
runApp(const MyApp());
}
but the the released app can not find dotEnv.develop file, however it exists in assets folder as this image shows:
and in console of chrome I get this error:
main.dart.js:41783 Error while trying to load an asset: Failed to load asset at "assets/dotEnv.develop" (404)
Failed to load resource: the server responded with a status of 404 (Not Found)
now my flutter sdk version is 3.0.5

The problem is that the dotEnv.develop is in the root folder and you're trying to load it from the assets folder.
Instead of:
await dotenv.load(fileName: "assets/dotEnv.develop");
Do the following instead:
await dotenv.load(fileName: "dotEnv.develop");

Create a .env file inside of the root of your project
and write this at it
API_URL=localhost:3000
then add the .env file to pubspec.yaml
assets:
- .env
this is resource

Related

Flutterconfig vairables are empty

I am making a flutter app in which I have to call some APIs.
I have added .env file but its still giving exception
FlutterConfig Variables are empty.
Ensure you have a .env file and you have loaded the variables.
How to solve this ?
Use the library flutter_dotenv
Add the .env file to your assets bundle in pubspec.yaml.
assets:
- .env
after in main.dart
void main() async {
...
await dotenv.load();
...
}
in app
dotenv.get('VAR_NAME')

How to use SQlite database using sqflite package in my Flutter App

I'm a beginner in flutter, I want to use SQLite database using sqflite package in my Flutter App,
when I use import 'package:path_provider/path_provider.dart';I have a compilation error saying Target of URI doesn't exist: 'package:path_provider/path_provider.dart'. Try creating the file referenced by the URI, or Try using a URI for a file that does exist.
and when I use
io.Directory documentDirectory = await getApplicationDocumentsDirectory();
I have a compilation error saying
Try correcting the name to the name of an existing method, or defining a method named 'getApplicationDocumentsDirectory'.```
Looks like you need to add path_provider dependency to your pubspec.yaml.
Add this:
dependencies:
...
path_provider: ^2.0.11
Then run
flutter pub get
More info: https://pub.dev/packages/path_provider

Flutter: Package can't load a string resource using rootBundle.loadString - asset indenting correct

I have a project that consumes another package on the drive using a relative path:
project1 (setup as a full flutter project with flutter create project1)
project2 (setup with flutter create --template=package
project1's packages.yaml does this:
dependancies:
project1:
path: ../project2
project2's packages.yaml does this:
flutter:
assets:
- lang/en.json
Which works and everything sees everything else and there is no complaint about that path for the asset and I've verified that it has exactly 2 spaces before assets: and exactly 4 actual spaces beofre - lang/en.json
The problem occurs when project2 tries to load lang/en.json like this in code form project2:
final jsonString =
await rootBundle.loadString('lang/en.json');
I get an "asset could not be loaded ${key}" on the loadString function.
if however I take exactly the same code and put it on project1 and copy the folder exactly and copy the exact same asset tag in packages.yaml, project1 has no problem loading the file. If I even leave the asset links on the project1 then project2 can load them just fine too.
Is this a bug, or am I doing something wrong with the package template version?
I have same issue. and I found the solution:
In project2, do some steps as below:
create assets folder
create lang folder(or any other folder name)
create json file in lang folder. Ex: en.json, vi.json...
in pubspec.yaml of project, you need to declare assets:
flutter:
assets:
- assets/lang/vi.json
- assets/lang/en.json
when using loadString then the path will be :
await rootBundle.loadString('packages/language_pack/assets/langen.json');
Note that packages is plural and language_pack is package name

How to include file assets to be used by your own package in flutter

I have a package where I'm trying to load a string from my rootBundle. This file is to be used within the package itself only as part of internal configuration. I cant see where i'm going wrong.
This is my folder structure:
my-package
--> lib
--> pubspec.yaml
--> assets/file.js
This is the content of my pubspec.yaml
flutter:
assets:
- assets/
- assets/file.js
This is how i'm calling it
String js = await rootBundle.loadString('assets/file.js');
I keep on getting unable to load asset
Unable to load asset: assets/file.js
had the same problem but solved it by doing this:
In your package's .yaml file, add this line to import the file/asset
assets:
- packages/<Package name>/assets/<File name>
Replace the <Package name> with the name of your package, and <File name> with the name of said file/asset.
Then place the file/asset in a folder under your lib directory of your package. The structure should look something like this:
my_package
--> lib
--> assets
file.js
Make sure to add any additional folders to the reference if necessary.
When referencing the file/asset in your app or package, use the same reference as in you .yaml file:
String js = await rootBundle.loadString('packages/<Package name>/assets/file.js');
For example, here I'm getting a .js file.

Unable to load asset (file the app created)

Why does this cause a crash?
final directory = await getApplicationDocumentsDirectory();
final testFile = File("${directory.path}/test.txt");
await testFile.writeAsString("Hello there!", flush: true);
final ByteData bytes = await rootBundle.load(testFile.path);
The rootBundle.load() call causes:
Unhandled Exception: Unable to load asset: /data/user/0/com.flutter.tests/app_flutter/test.txt
BUT if I do a hot reload then it works fine until I restart the app.
I have a dependency path_provider: any in pubspec.yaml which is needed for the getApplicationDocumentsDirectory().
rootBundle only contains assets that were specified by pubspec.yaml and were built with your application. It's not supposed to access files created within file system. Use File or Image classes to open created files.
final bytes = testFile.readAsBytesSync();
The rootBundle contains the resources that were packaged with the
application when it was built. To add resources to the rootBundle for
your application, add them to the assets subsection of the flutter
section of your application's pubspec.yaml manifest.