【发布时间】: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