본문 바로가기
프레임워크/flutter

[Error] No MaterialLocalizations found - MyApp widgets require MaterialLocalizations to be provided by a Localizations widget ancestor

by seongjko 2023. 10. 9.
728x90

이런 식으로 동작하는 팝업창 연습용 앱을 만들기 위해서 

 

이런 식으로 코드를 짰다.

void main() => runApp(MyHomePage());


class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
      appBar: AppBar(
        title: const Text("Test Title"),
      ),
      body: Container(
          child: Center(
              child: TextButton(
                onPressed: () {
                  showDialog(
                    context: context,
                    builder: (BuildContext con) {
                      return AlertDialog(
                        title: const Text("Dialog Title"),
                        content: Container(
                          child: const Text(
                            "Dialog content",
                          ),
                        ),
                        actions: [
                          TextButton(
                            onPressed: () => Navigator.of(context).pop(),
                            child: const Text("Close"),
                          )
                        ]
                      );
                    }
                  );
                },
                  child: const Text("Button"),
      ))),
    ),
    );
  }
}

 

그랬더니 실행 과정 중에 

No MaterialLocalizations found - MyApp widgets require MaterialLocalizations to be provided by a Localizations widget ancestor

이런 에러가 나왔다.

 

그래서 이 부분을 

void main() => runApp(MyHomePage());

이렇게 바꿨더니 정상적으로 작동한다.

void main() {
  runApp(MaterialApp(home: MyHomePage()));
}

 

참고 

https://stackoverflow.com/questions/56275595/no-materiallocalizations-found-myapp-widgets-require-materiallocalizations-to

 

No MaterialLocalizations found - MyApp widgets require MaterialLocalizations to be provided by a Localizations widget ancestor

I was just trying to create an app with button which shows an alert message when the button is pressed. But it gives me this error(Mentioned below). I wrote this code by taking reference of this...

stackoverflow.com

 

반응형