by @kodeazy

flutter No MaterialLocalizations found?

Home » flutter » flutter No MaterialLocalizations found?

I was trying to add an Alert in flutter but getting below error.

Error: No MaterialLocalizations found.
DynamicData widgets require MaterialLocalizations to be provided by a Localizations widget ancestor.
The material library uses Localizations to generate messages, labels, and abbreviations.
To introduce a MaterialLocalizations, either use a MaterialApp at the root of your application to include them automatically, or add a Localization widget with a MaterialLocalizations delegate.
The specific widget that could not find a MaterialLocalizations ancestor was:
  DynamicData
The ancestors of this widget were:
  [root]
    at Object.throw_ [as throw] (http://localhost:49240/dart_sdk.js:5074:11)
    at http://localhost:49240/packages/flutter/src/material/icon_button.dart.lib.js:49325:19
    at Object.debugCheckHasMaterialLocalizations (http://localhost:49240/packages/flutter/src/material/icon_button.dart.lib.js:49332:24)
    at MaterialLocalizations.of (http://localhost:49240/packages/flutter/src/material/icon_button.dart.lib.js:49371:20)
    at alert.Alert.new.show (http://localhost:49240/packages/rflutter_alert/src/alert.dart.lib.js:868:352)
    at show.next (<anonymous>)
    at runBody (http://localhost:49240/dart_sdk.js:40657:34)
    at Object._async [as async] (http://localhost:49240/dart_sdk.js:40688:7)
    at alert.Alert.new.show (http://localhost:49240/packages/rflutter_alert/src/alert.dart.lib.js:867:20)
    at http://localhost:49240/packages/quiz_appexample/main.dart.lib.js:296:119
    at main._DynamicDataState.new.setState (http://localhost:49240/packages/flutter/src/widgets/widget_inspector.dart.lib.js:13410:22)
    at http://localhost:49240/packages/quiz_appexample/main.dart.lib.js:287:30
    at [_handleTap] (http://localhost:49240/packages/flutter/src/material/icon_button.dart.lib.js:41869:31)
    at tap.TapGestureRecognizer.new.invokeCallback (http://localhost:49240/packages/flutter/src/gestures/recognizer.dart.lib.js:190:18)
    at tap.TapGestureRecognizer.new.handleTapUp (http://localhost:49240/packages/flutter/src/gestures/tap.dart.lib.js:411:42)
    at [_checkUp] (http://localhost:49240/packages/flutter/src/gestures/tap.dart.lib.js:217:12)
    at tap.TapGestureRecognizer.new.handlePrimaryPointer (http://localhost:49240/packages/flutter/src/gestures/tap.dart.lib.js:166:23)
    at tap.TapGestureRecognizer.new.handleEvent (http://localhost:49240/packages/flutter/src/gestures/recognizer.dart.lib.js:426:16)
    at [_dispatch] (http://localhost:49240/packages/flutter/src/gestures/pointer_router.dart.lib.js:86:9)
    at http://localhost:49240/packages/flutter/src/gestures/pointer_router.dart.lib.js:112:26
    at LinkedMap.new.forEach (http://localhost:49240/dart_sdk.js:27742:11)
    at [_dispatchEventToRoutes] (http://localhost:49240/packages/flutter/src/gestures/pointer_router.dart.lib.js:110:29)
    at pointer_router.PointerRouter.new.route (http://localhost:49240/packages/flutter/src/gestures/pointer_router.dart.lib.js:105:37)
    at binding$5.WidgetsFlutterBinding.new.handleEvent (http://localhost:49240/packages/flutter/src/gestures/binding.dart.lib.js:367:26)
    at binding$5.WidgetsFlutterBinding.new.dispatchEvent (http://localhost:49240/packages/flutter/src/gestures/binding.dart.lib.js:355:24)
    at binding$5.WidgetsFlutterBinding.new.dispatchEvent (http://localhost:49240/packages/flutter/src/rendering/layer.dart.lib.js:5438:13)
    at [_handlePointerEventImmediately] (http://localhost:49240/packages/flutter/src/gestures/binding.dart.lib.js:331:14)
    at binding$5.WidgetsFlutterBinding.new.handlePointerEvent (http://localhost:49240/packages/flutter/src/gestures/binding.dart.lib.js:305:43)
    at [_flushPointerEventQueue] (http://localhost:49240/packages/flutter/src/gestures/binding.dart.lib.js:295:14)
    at [_handlePointerDataPacket] (http://localhost:49240/packages/flutter/src/gestures/binding.dart.lib.js:286:54)
    at Object.invoke1 (http://localhost:49240/dart_sdk.js:190888:7)
    at _engine.EnginePlatformDispatcher.__.invokeOnPointerDataPacket (http://localhost:49240/dart_sdk.js:171345:15)
    at [_onPointerData] (http://localhost:49240/dart_sdk.js:172281:49)
    at http://localhost:49240/dart_sdk.js:172719:28
    at http://localhost:49240/dart_sdk.js:172675:16
    at loggedHandler (http://localhost:49240/dart_sdk.js:172380:11)

Below is my example code
main.dart

import 'package:flutter/material.dart';
import 'package:rflutter_alert/rflutter_alert.dart';
void main() {
  runApp(MyApp());
}
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          body: FlatButton(onPressed: (){
            Alert(context: context, title: "Hurraih", desc: "Button Clicked Successfully.").show();

          }, child: Text("Example Button"),
            color: Colors.red,)
      ),

    );
  }
}

pubspec.yaml
Here I added rflutter_alert dependency for Alert.

dependencies:
  flutter:
    sdk: flutter
  rflutter_alert: ^2.0.4

In the above code we added MaterialApp widget inside MyApp class.
Error is because the context we are passing inside Alert widget does not have MaterialLocalizations inside the widget tree.
MaterialApp widget internally has MaterialLocalizations.
So to resolve the issue we call the MyApp() class from MaterialApp widget as MaterialApp consists of MaterialLocalizations the error gets resolved
Follow below Steps to resolve the issue

  • Remove MaterialApp widget from MyApp class.
  • Add MaterialApp widget in main method.
  • Call the MyApp class from main method.

Below is the example syntax.

void main() {
  runApp(MaterialApp(
    home: MyApp(),
  ));
}

Below is the complete code after resolviong the error.

main.dart

import 'package:flutter/material.dart';
import 'package:rflutter_alert/rflutter_alert.dart';
void main() {
  runApp(MaterialApp(
    home: MyApp(),
  ));
}
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return  Scaffold(
          body: FlatButton(onPressed: (){
            Alert(context: context, title: "Hurraih", desc: "Button Clicked Successfully.").show();

          }, child: Text("Example Button"),
            color: Colors.red,)
      );
  }
}