gpt4 book ai didi

flutter - 如何集成flutter dotenv?

转载 作者:行者123 更新时间:2023-12-04 17:19:35 29 4
gpt4 key购买 nike

我正在尝试添加 flutter_dotenv在我的项目中。我尝试将 .env 文件添加到 PROJECTNAME/filename.env 和 PROJECTNAME/lib/filename.env。我不确定项目的根在哪里。然后当我尝试运行该项目时,就会出现这种情况。

Launching lib\main.dart on V2026 in debug mode...
lib\main.dart:1
Error detected in pubspec.yaml:
No file or variants found for asset: .env.
3

FAILURE: Build failed with an exception.

* Where:
Script 'C:\src\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 904

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:\src\flutter\bin\flutter.bat'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1m 28s
Exception: Gradle task assembleDebug failed with exit code 1
我也发现了这个错误
[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: Instance of 'FileNotFoundError'
E/flutter (32366): #0 _getEntriesFromFile
package:flutter_dotenv/src/dotenv.dart:76
E/flutter (32366): <asynchronous suspension>
E/flutter (32366): #1 load
package:flutter_dotenv/src/dotenv.dart:52
E/flutter (32366): #2 main
package:SOSMAK/main.dart:19
E/flutter (32366): <asynchronous suspension>
E/flutter (32366): #3 _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:231:25)
E/flutter (32366): #4 _rootRun (dart:async/zone.dart:1190:13)
E/flutter (32366): #5 _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (32366): #6 _runZoned (dart:async/zone.dart:1630:10)
E/flutter (32366): #7 runZonedGuarded (dart:async/zone.dart:1618:12)
E/flutter (32366): #8 _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:223:5)
E/flutter (32366): #9 _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:301:19)
E/flutter (32366): #10 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
我的 pubspec.yaml
名称:项目名称
说明: 说明
version: 1.0.0+1

environment:
sdk: ">=2.7.0 <3.0.0"

dependencies:
flutter:
sdk: flutter
provider: ^4.3.2+1
cloud_firestore: ^0.16.0
firebase_core: "^0.7.0"
firebase_auth: "^0.20.0+1"
firebase_messaging: ^6.0.16
flutter_local_notifications: ^4.0.0
google_fonts: ^1.1.0
nima: ^1.0.0
url_launcher: ^5.7.10
flutter_dotenv: ^2.1.0

dev_dependencies:
flutter_test:
sdk: flutter

flutter:
uses-material-design: true
assets:
- assets/
- .env
它在“- .env”中说
The asset .env does not exist.
Try creating the file or fixing the path to the file.
我的主要.dart
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
await DotEnv.load(fileName: ".env");
runApp(MyApp());
}

最佳答案

该文件夹应如下所示 Folder Structure ,因为您已经添加了 .env在 pubspec.yaml 中,我没有看到任何其他问题。至于最近发布的dotenv包裹,await load();如果文件名是 .env 将执行所有操作.我认为错误是因为文件目录可能有点错误

关于flutter - 如何集成flutter dotenv?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67050527/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com