need solution
Hi i am working in flutter web app.need to use the env.so that i have used the flutter_dotenv package. i created .env file and add all credentials to that. its working on local very fine. When i deployed the app into iis server the app is not loaded. After removing the flutter_dotenv package it app works normally. what is the steps or solution to fix this issue?
I've experienced the same issue with the AWS server. It works fine on my local WAMPP server, but after hosting it on AWS, it's not functioning properly.
I suspect this might be due to a server update, but I'm not entirely sure. The .env file is being loaded as index.html instead of a .env file.
Could you try changing the MIME type?
If you need the server to recognize the .env files for internal purposes, you might consider adding a MIME type for .env files. Here's how you can do it:
Open IIS Manager: Go to your server or site. Select the Server or Site: In the left-hand Connections pane, select the server or site where you want to configure the MIME type. Open MIME Types: In the Features View, double-click on "MIME Types". Add MIME Type: On the right-hand side, click on "Add..." File name extension: .env MIME type: text/plain (or another appropriate type)
Please try to change the way you fetch .env file
Because .env file loading as content-type: text/html. but it should load as content-type: text/plain
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:http/http.dart' as http;
Future<String> fetchEnvFile(String url) async {
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
return response.body;
} else {
throw Exception('Failed to load .env file');
}
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
// Fetch the .env file from Server
final envContent = await fetchEnvFile('https://your-base-url/.env');
// Load the environment variables from the fetched content
await dotenv.load(fileInput: envContent);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter S3 .env Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
// Access environment variables using dotenv
final apiUrl = dotenv.env['API_URL'] ?? 'Default API URL';
return Scaffold(
appBar: AppBar(
title: Text('Flutter S3 .env Example'),
),
body: Center(
child: Text('API URL: $apiUrl'),
),
);
}
}