Logo

Using envvault with Dart/Flutter

Note: Please ensure that you have completed the previous steps

Prerequisites

  • Dart SDK installed
  • envvault CLI tool installed
  • An existing Dart project

Usage

Running Your Application

To run your Dart application with environment variables from envvault:

$ envvault run --env=dev dart run

Caching Environment Variables

For better performance, you can cache your environment variables:

$ envvault run --env=dev -c -- dart run

Example Implementation

Here is how to set up a basic Dart web server with envvault:

// main.dart
import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as io;
import 'dart:io' show Platform;

void main() async {
  // Your environment variables are automatically loaded
  final port = Platform.environment['PORT'] ?? '8080';
  final dbUrl = Platform.environment['DB_URL'];

  final handler = const shelf.Pipeline()
      .addMiddleware(shelf.logRequests())
      .addHandler(_handleRequest);

  final server = await io.serve(
    handler,
    'localhost',
    int.parse(port),
  );

  print('Serving at http://${server.address.host}:${server.port}');
}

shelf.Response _handleRequest(shelf.Request request) {
  return shelf.Response.ok('Hello from envvault!');
}

// pubspec.yaml
name: my_app
dependencies:
  shelf: ^1.4.0