skip to Main Content

I am new to flutter and no clear documents for creating a config file.What is the standard way of writing configuration in flutter similar to one below which we write in appsettings.json(.net core)?

   "Travel": {
     "EndPoint": "https://api.openai.com/v1/threads",
     "ApiKey": "sk-123",
     "AssistantId": "asst_123"
   },
   "Home": {
     "EndPoint": "https://api.openai.com/v1/threads",
     "ApiKey": "sk-1234",
     "AssistantId": "asst_1234"
   },
   "Edu": {
     "EndPoint": "https://api.openai.com/v1/threads",
     "ApiKey": "sk-123456",
     "AssistantId": "asst_12345"
     }
 }```

2

Answers


  1. You can create a Dart class to represent the structure and store the configuration data. Here’s how you can do it:

    class AppConfig {
      static const Map<String, Map<String, String>> config = {
        "Travel": {
          "EndPoint": "https://api.openai.com/v1/threads",
          "ApiKey": "sk-123",
          "AssistantId": "asst_123"
        },
        "Home": {
          "EndPoint": "https://api.openai.com/v1/threads",
          "ApiKey": "sk-1234",
          "AssistantId": "asst_1234"
        },
        "Edu": {
          "EndPoint": "https://api.openai.com/v1/threads",
          "ApiKey": "sk-123456",
          "AssistantId": "asst_12345"
        }
      };
    }
    
    

    In this Dart class:

    AppConfig represents the configuration class.

    config is a static constant map containing the configuration data.

    The keys of the outer map represent different categories (Travel, Home, Edu).

    Each category contains a nested map with configuration details (EndPoint, ApiKey, AssistantId).
    You can then access this configuration throughout your app like so:

    String travelEndPoint = AppConfig.config["Travel"]["EndPoint"];
    String travelApiKey = AppConfig.config["Travel"]["ApiKey"];
    String travelAssistantId = AppConfig.config["Travel"]["AssistantId"];
    
    
    Login or Signup to reply.
  2. Several methods commonly used in Flutter:

    • You can use JSON in your project.

    First add your JSON config file in your project, somewhere like: config/server.json. Then update the assets in pubspec.yaml:

      assets:
        - config/server.json
    

    Now, you can load your JSON:

    import 'dart:convert';
    import 'package:flutter/services.dart' show rootBundle;
    
    Future<void> loadConfig() async {
      final jsonString = await rootBundle.loadString('config/appsettings.json');
      final Map<String, dynamic> jsonMap = json.decode(jsonString);
      // Use the jsonMap for configuration
    }
    
    • You can create a Dart file that contains config settings as constants or a class with static properties. (My preferred way):
    class Config {
      final String title;
      final String endPoint;
      final String apiKey;
      final String assisstantId;
    
      const Config({
        required this.title,
        required this.endPoint,
        required this.apiKey,
        required this.assisstantId,
      });
    }
    

    And the config repository is like:

    class ConfigRepository {
      static const travel = Config(
        title: 'Travel',
        endPoint: 'https://api.openai.com/v1/threads',
        apiKey: 'sk-123',
        assisstantId: 'asst_123',
      );
    
      static const home = Config(
        title: 'Home',
        endPoint: 'https://api.openai.com/v1/threads',
        apiKey: 'sk-123',
        assisstantId: 'asst_123',
      );
    }
    
    // To access all list together
    static List<Config> get list => [travel, home];
    
    // Find property which has the specific name
    static Config findByName(String name) => list.firstWhere(
       (l) => l.name == name,
       orElse: () => list[0],
    );
    

    This way you can access your data for instance travel data by using ConfigRepository.travel.

    This way you can add some method like finding the data by name (or anyth other properties that you want). (as I mentioned in the class.)

    • You may want to search for environment variable in flutter like looking at the packages like flutter_dotenv, envied, etc.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search