How to fix missing MaterialLocalizations using Riverpod with Flutter?

  Kiến thức lập trình

This is how my app is initialised using Riverpod with Flutter:

void main() {
  runApp(const ProviderScope(child: MyApp()));
}

class MyApp extends ConsumerStatefulWidget {
  const MyApp({super.key});

  @override
  MyAppState createState() => MyAppState();
}

class MyAppState extends ConsumerState<MyApp> {

@override
  Widget build(BuildContext context) {
    // some logic here
    return MaterialApp(
        theme: ThemeData(
          fontFamily: 'Roboto',
        ),
        home: SafeArea(
            child: Scaffold(
                extendBodyBehindAppBar: true,
                appBar: appBar,
                drawer: Drawer(
                  backgroundColor: Colors.white,
                  child: ListView(
                    padding: EdgeInsets.zero,
                    children: [
                      DrawerHeader(...),
                      ),
                      ... other ListTiles ...,
                      ListTile(
                        leading: const Icon(
                          Icons.person,
                        ),
                        title: const Text('About'),
                        onTap: () {
                          _showAboutDialog();
                          // Navigator.pop(context);
                        },
                      ),
                    ],
                  ),
                ),
}

When the “about” ListTile is clicked, an alert dialog should be open. However I get the error

E/flutter ( 5236): MyApp widgets require MaterialLocalizations to be provided by a Localizations widget ancestor.
E/flutter ( 5236): The material library uses Localizations to generate messages, labels, and abbreviations.
E/flutter ( 5236): 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.
E/flutter ( 5236): The specific widget that could not find a MaterialLocalizations ancestor was:
E/flutter ( 5236):   MyApp
E/flutter ( 5236): The ancestors of this widget were:
E/flutter ( 5236):   UncontrolledProviderScope
E/flutter ( 5236):   ProviderScope
E/flutter ( 5236):   MediaQuery
E/flutter ( 5236):   _MediaQueryFromView
E/flutter ( 5236):   _PipelineOwnerScope
E/flutter ( 5236):   _ViewScope
E/flutter ( 5236):   _RawView-[_DeprecatedRawViewKey FlutterView#7755d]
E/flutter ( 5236):   View
E/flutter ( 5236):   [root]

even if the app is wrapper with MaterialApp(...) and ProviderScope (which might be the cause). Any help?

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT