투케이2K

249. (TWOK/ERROR) [Flutter] Unhandled Exception .. accessed before the binding was initialized 본문

투케이2K 에러관리

249. (TWOK/ERROR) [Flutter] Unhandled Exception .. accessed before the binding was initialized

투케이2K 2024. 6. 28. 12:47
반응형

[환경 설정 및 설명]

프로그램 : Flutter

설 명 : [Flutter] Unhandled Exception .. accessed before the binding was initialized

 

[에러 원인]

1. 바이너리 , 네트워크 등 사전) 서비스 바인딩이 초기화 되지 않은 상태에서 사용하는 경우 발생 이슈

2. 애플리케이션의 바인딩이 초기화되지 않은 상태에서 비동기 작업을 수행하는 경우 발생 이슈

Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.
 

[해결 방법]

1. 플러터 Dart 프로젝트에서 main 부분 시작 실행 부분에 WidgetsFlutterBinding 초기화 수행

 

void main() async {
  print("");
  print("-------------------------------------------------------");
  print("Application :: Start");
  print("-------------------------------------------------------");
  print("");

  // [WidgetsFlutterBinding 초기화]
  WidgetsFlutterBinding.ensureInitialized();

  // [텍스트 파일 읽기 수행]
  try {

    // [String 문자열 읽기]
    var contents = await rootBundle.loadString('assets/test.txt');

    print("");
    print("-------------------------------------------------------");
    print("contents :: ${contents}");
    print("-------------------------------------------------------");
    print("");
  }
  catch (e) {
    print("");
    print("-------------------------------------------------------");
    print("catch :: ${e}");
    print("-------------------------------------------------------");
    print("");
  }

}

 

반응형
Comments