【问题标题】:I am trying to link google api to my project using the file.env .I am getting the error file not found我正在尝试使用 file.env 将 google api 链接到我的项目。我收到错误文件未找到
【发布时间】:2021-08-10 13:52:30
【问题描述】:

我正在尝试使用颤振制作搜索酒店应用程序。 我为此使用谷歌 API。 我正在使用 dotenv 包并制作了一个 .env 文件,其中存在 API 的密钥。 如我的图片所示,未加载 env 文件。 这是我的代码

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.
  }
}

我收到以下错误:- 尝试加载资产时出错:无法在“assets/.env”加载资产 (404) 错误:“FileNotFoundError”的实例 我已经更新了我的 Pubsback.yaml 文件 [![在此处输入图片描述][1]][1]

  [1]: https://i.stack.imgur.com/MrTYW.png

【问题讨论】:

    标签: android flutter android-studio dart


    【解决方案1】:

    而不是await DotEnv().load(fileName: '.env'); 试试这个await dotenv.load(fileName: ".env");

    【讨论】:

      【解决方案2】:

      .env 文件应该在项目的根目录中。 yaml 应该是这样的

      flutter:
        generate: true
        assets:
          - assets/images/
          - assets/launcher_icons/
          - .env
      

      【讨论】:

      • 我会试试这个谢谢!!。
      猜你喜欢
      • 2022-12-22
      • 1970-01-01
      • 2021-08-09
      • 2020-05-31
      • 1970-01-01
      • 2022-12-02
      • 2020-12-12
      • 2021-05-21
      • 2021-03-09
      相关资源
      最近更新 更多