简体   繁体   English

我正在尝试使用 file.env 将 google api 链接到我的项目。我收到了未找到的错误文件

[英]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.

相关问题 我正在尝试解析桌面上的xml文件。 我收到了一个错误 - I am trying to parse an xml file on my desktop. I am getting an error 我在我的项目中收到一个已弃用的 API 错误 - i am getting a deprecated API error in my project 当我尝试通过我的应用程序访问 api 时出现 403 错误 - I am getting an 403 error when I am trying to access an api through my app 为什么在Android的ExifInterface上出现File not found错误? - Why am I getting File not found error for ExifInterface with Android? 我试图在单击按钮时选择 PDF 文件,但出现错误 - I am trying to select PDF file on button click but I am getting an error 当我尝试运行Facebook SDK的示例时,我收到无效的Apk文件错误 - when i am trying to run samples of Facebook SDK I am getting Invalid Apk file error 我的 build.gradle 文件出现错误 - I am getting an error in my build.gradle file 尝试在我的 Android 应用程序项目中添加电话身份验证时出现错误 - I am getting error when trying to add phone authentication in my Android app project Android Studio:为什么我在全新的 Google Maps API 项目上出现多 dex 错误? - Android Studio: Why am i getting multi dex error on brand new Google Maps API project? 我正在尝试在应用程序中使用Clarifai api,但收到一条错误消息,提示“无法解析符号” - I am trying to use Clarifai api in my application, but getting an error saying that “cannot resolve symbol”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM