I am trying to make search hotel app using flutter.
I am using google API for this .
I am using a dotenv package and made a .env file where the key of API is present .
The env file is not getting loaded as shown in my picture.
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]
[1]: https://i.stack.imgur.com/MrTYW.png
2
Answers
.env file should be in the root of the project. The yaml should look like this
Instead of
await DotEnv().load(fileName: '.env');
try this
await dotenv.load(fileName: ".env");