How to Solve ’No MaterialLocalizations found’ Error in Flutter

You may get 'No MaterialLocalizations found' error while showing dialog using showDialog() class in Flutter. To solve this exception, see the example below and learn how to solve this issue. There are two solutions mentioned below:

Solution One: Create new class for 'home' property of MaterialApp() class:

import 'package:flutter/material.dart';
void main(){
  runApp(MyApp());
}

class MyApp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Home() //create new class for 'home' property of MaterialApp()
                   //to escape 'No MaterialLocalizations found' error
    );
  }
}

class Home extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return  Scaffold(
          body: Center( 
                 child: ElevatedButton(
                   child:Text("Show Alert"),
                   onPressed: (){
                      showDialog(context: context, builder: (BuildContext context){
                         return AlertDialog(
                             title: Text("ERROR"),
                             content: Text("No MaterialLocalizations found Error"),
                         );
                      });
                   },
                 ),
             )
       );
  }
}

Solution Two: Put MaterialApp() inside runApp() and create new class for 'home' property.

import 'package:flutter/material.dart';
void main(){
  runApp(
    MaterialApp( 
      home: MyApp() 
    )
  );
}
/* 
place MaterialApp() widget on runApp() and create
new class for its 'home' property
to escape 'No MaterialLocalizations found' error
*/

class MyApp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return  Scaffold(
          body: Center( 
                 child: ElevatedButton(
                   child:Text("Show Alert"),
                   onPressed: (){
                      showDialog(context: context, builder: (BuildContext context){
                         return AlertDialog(
                             title: Text("ERROR"),
                             content: Text("No MaterialLocalizations found Error"),
                         );
                      });
                   },
                 ),
             )
       );
  }
}

If you see both solutions, you may have guessed what the main issue here. The issue is putting child widget on 'home' property of MaterialApp() widget without creating new widget class. 

This is the way you can solve 'No MaterialLocalizations found' error exception in Flutter App. 

No any Comments on this Article


Please Wait...