Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

need solution #106

Open
op0318 opened this issue Jul 30, 2024 · 2 comments
Open

need solution #106

op0318 opened this issue Jul 30, 2024 · 2 comments

Comments

@op0318
Copy link

op0318 commented Jul 30, 2024

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?

@govarthananve
Copy link

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)

@govarthananve
Copy link

govarthananve commented Aug 6, 2024

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'),
      ),
    );
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants