[Flutter / Main 함수 내 비동기처리 오류 해결] MethodChannel를 Main에서 사용하기

Min·2022년 4월 6일
0

문제인식

앱이 구동되면서 MethodChannel이 init이 될 수 있도록 main()에 init 코드를 구현하고자 하지만 아래와 같이 오류가 발생한다.

Cannot use this MethodChannel before the binary messenger has been initialized. This happens when you invoke platform methods before the WidgetsFlutterBinding has been initialized. You can fix this by either calling WidgetsFlutterBinding.ensureInitialized() before this or by passing a custom BinaryMessenger instance to MethodChannel().)

해결방안

void main() {
  WidgetsFlutterBinding.ensureInitialized(); // 이 코드를 추가한다.
  final _channel = Get.put(MethodChannelGetXController(), permanent: true);
  _channel.methodChannelInit();
  runApp(const MyApp());
}
profile
Flutter 개발자

0개의 댓글