min_chan님의 블로그

[Flutter] - ProviderNotFoundException 본문

Flutter

[Flutter] - ProviderNotFoundException

min_chan 2025. 1. 9. 16:29
 
 

 

 


  • 작성 코드
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

// 기존
class MyApp extends StatelessWidget {
  //현재 페이지 설정

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: ChattingPage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
        create: (context) => ChattingProvider(), child: ChattingPage());
  }
}

  • 에러 메시지

ProviderNotFoundException (Error: Could not find the correct Provider above this ChattingPage Widget


  • 에러 발생 원인
    • - Provider를 만들기 전에 home위치를 ChattingPage로 바로 연결시켰는데, Provider를 만들면서 ChattingPage를 설정해줘서 생긴 에러라고 생각했다.

  • 수정 코드
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

// 기존
class MyApp extends StatelessWidget {
  //현재 페이지 설정

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
        create: (context) => ChattingProvider(), child: ChattingPage());
  }

  • 성공적으로 작동한다 ^^*