본문 바로가기

Flutter

[j Flutter] Viewmodel 사용시 WidgetsFlutterBinding.ensureInitialized() 에러해결방법

728x90
반응형

 Before

 

main.dart에서 runApp부분에 viewmodel을 먼저 선언하고 있었다.

void main() {
  runApp(ChangeNotifierProvider.value(value: CarModel(), child: MyApp()));
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
    );
  }
}

After

 

runApp()전에 WidgetsFlutterBinding.ensureInitialized(); 를 선언해준다.

 

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(ChangeNotifierProvider.value(value: CarModel(), child: MyApp()));
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
    );
  }
}

 

WidgetsFlutterBinding.ensureInitialized() 관련 문서 링크

 

api.flutter.dev/flutter/widgets/WidgetsFlutterBinding/ensureInitialized.html

 

ensureInitialized method - WidgetsFlutterBinding class - widgets library - Dart API

WidgetsBinding ensureInitialized () Returns an instance of the WidgetsBinding, creating and initializing it if necessary. If one is created, it will be a WidgetsFlutterBinding. If one was previously initialized, then it will at least implement WidgetsBindi

api.flutter.dev

 

728x90
반응형