티스토리 뷰
          Programming Framework/Flutter
          
        [Flutter] Yaml 파일 Decode/DeSerialize(Yaml 객체화)
HoZang 2024. 5. 26. 13:40[Flutter] Yaml 파일 Decode/DeSerialize(Yaml 객체화)
아래 코드는 yaml 파일로부터 읽어온 데이터를 GetIt ServiceLocator에 저장 하는 과정까지의 코드이다.
http 통신에 사용할 서버 정보를 yaml파일로 설정하기 위한 예시이다.
import 'package:get_it/get_it.dart';
import 'package:hello_world/services/config_service.dart';
import "package:flutter/services.dart" as services;
import "package:yaml/yaml.dart";
Future<void> serviceConfig() async {
  final String data = await services.rootBundle.loadString('server.yaml');
  final YamlMap mapData = loadYaml(data);
  final Server server = Server.fromYaml(mapData);
  final ConfigService configService = ConfigService(server: server);
  GetIt.I.registerSingleton<ConfigService>(configService);
}
class Server {
  const Server({
    required this.protocol,
    required this.host
  });
  final String protocol;
  final String host;
  factory Server.fromYaml(YamlMap yaml) => Server(
      protocol: yaml['protocol'] ?? '',
      host: yaml['host'] ?? ''
  );
}
class ConfigService {
  ConfigService({required this.server});
  final Server server;
}'Programming Framework > Flutter' 카테고리의 다른 글
| [Flutter] getApplicationCacheDirectory 폴더 위치 (0) | 2024.05.27 | 
|---|---|
| [Flutter] Windows App getApplicationCacheDirectory 경로 변경 (0) | 2024.05.27 | 
| [Flutter] Splash Screen(앱 시작 화면) 만들기 (0) | 2024.05.26 | 
| [Flutter] GetIt 패키지를 이용한 Service Locator 전역 상태 관리 (0) | 2024.05.25 | 
| [Flutter] JsonSerialize(encode / decode) (0) | 2024.05.24 | 
                      공지사항
                      
                  
                
                  
                  
                    최근에 올라온 글
                    
                
                  
                  
                    최근에 달린 댓글
                    
                
                  
                  - Total
- Today
- Yesterday
                    TAG
                    
                
                  
                  - 전자정부
- 스프링
- Fedora
- jQuery
- php
- Java
- eGovFrame
- Spring
- Flutter
- .NET Core
- dotnet core
- Windows
- JSP
- egov
- asp.net core
- c#
- Entity Framework Core
- MySQL
- 자바
- 하이원
- Eclipse
- CentOS
- high1
- 이클립스
- MariaDB
- DART
- egoverment
- ubuntu
- express
- Linux
| 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 | 
| 12 | 13 | 14 | 15 | 16 | 17 | 18 | 
| 19 | 20 | 21 | 22 | 23 | 24 | 25 | 
| 26 | 27 | 28 | 29 | 30 | 31 | 
                    글 보관함