[英]I am trying to link google api to my project using the file.env .I am getting the error file not found
I am trying to make search hotel app using flutter.我正在尝试使用颤振制作搜索酒店应用程序。 I am using google API for this .
我为此使用谷歌 API。 I am using a dotenv package and made a .env file where the key of API is present .
我正在使用 dotenv 包并制作了一个 .env 文件,其中存在 API 的密钥。 The env file is not getting loaded as shown in my picture.
如我的图片所示,env 文件未加载。 Here is my code
这是我的代码
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
Future<void> main()
async {
await DotEnv().load(fileName: '.env');//this env file
runApp(RestaurantSearchApp());
}
class RestaurantSearchApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.indigo,
),
home: SearchPage(title: 'Flutter Demo Home Page'),
);
}
}
class SearchPage extends StatefulWidget {
SearchPage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_SearchPageState createState() => _SearchPageState();
}
class _SearchPageState extends State<SearchPage> {
final _formKey = GlobalKey<FormState>();
var _autoValidate = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Form(
key: _formKey,
autovalidate:_autoValidate,
child: Column(
children: [
TextFormField(
decoration: InputDecoration(
prefixIcon: Icon(Icons.search),
hintText: 'Enter search',
border: OutlineInputBorder(),
filled: true,
errorStyle:TextStyle(fontSize:15),
),
validator:(value){
if(value!.isEmpty){
return 'Please Enter a Search Item';
}
return null;
}
),
SizedBox(height:10),
SizedBox(
width: double.infinity,
child: RawMaterialButton(
onPressed: () {
final isValid = _formKey.currentState!.validate();
if(isValid){
//to do search
}
else
{
//todo set autovalidate = true
setState((){
_autoValidate = true;
});
}
},
fillColor: Colors.indigo,
shape:RoundedRectangleBorder(
borderRadius:BorderRadius.circular(5),
),
child:Padding(
padding: const EdgeInsets.all(15),
child: Text(
'Search',
style: TextStyle(
color: Colors.white,
fontSize: 18,
),
),
),
),
),
],
),
),
],
),
),
); // This trailing comma makes auto-formatting nicer for build methods.
}
}
I am geting the below error :- Error while trying to load an asset: Failed to load asset at "assets/.env" (404) Error: Instance of 'FileNotFoundError' I have update my Pubsback.yaml file [![enter image description here][1]][1]我收到以下错误:- 尝试加载资产时出错:无法在“assets/.env”(404)处加载资产错误:“FileNotFoundError”的实例我已经更新了我的 Pubsback.yaml 文件 [![enter image]此处描述][1]][1]
[1]: https://i.stack.imgur.com/MrTYW.png
.env file should be in the root of the project. .env 文件应该在项目的根目录中。 The yaml should look like this
yaml 应该是这样的
flutter:
generate: true
assets:
- assets/images/
- assets/launcher_icons/
- .env
Instead of await DotEnv().load(fileName: '.env');
而不是
await DotEnv().load(fileName: '.env');
try this await dotenv.load(fileName: ".env");
试试这个
await dotenv.load(fileName: ".env");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.