Flutter为AppLocalization返回空值,of(上下文)

nnt7mjpx  于 2023-03-13  发布在  Flutter
关注(0)|答案(4)|浏览(177)

我正在使用Flutter构建一个Web应用程序,我想在我的新应用程序上使用flutter的国际化特性。我正在使用Flutter-Tutorial,并尝试使用arb-file设置app-title。正如教程中提到的,为“en”和“de”正确创建了app_localization. dart文件。然而,我在以下代码中遇到了一个空指针异常。

import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'MyApp',
      localizationsDelegates: [
        AppLocalizations.delegate, // Post-EDIT due to croxx5f
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: [
        Locale('de', ''),
        Locale('en', ''),
      ],
      theme: ThemeData(
        primarySwatch: Colors.red,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text(AppLocalizations.of(context)!.appTitle),
        ),
        body: Text(AppLocalizations.of(context)!.appTitle)
      ),
    );
  }
}

实际上,AppLocalizations.of(上下文)返回空值。

wxclj1h5

wxclj1h51#

您应该在MaterialApp中添加AppLocalizations

MaterialApp(
...
      localizationsDelegates: const [
        AppLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
      ],
      supportedLocales: AppLocalizations.supportedLocales,
...
vxqlmq5t

vxqlmq5t2#

在我的案例中,出于测试目的,将Scaffold()用作MaterialApp()home:导致了该问题。
一旦我创建了一个单独的HomePage()小部件并将其用作home: HomePage(),问题就没有了。

  • 当我将Scaffold()作为MaterialApp的直接子对象时,会认为AppLocalizationDelegate没有可用的父上下文,因为MaterialApp是所有底层小部件的主要上下文构建器。*

错误代码:

return MaterialApp(
                home: Scaffold(
                  body: Txt(text: AppLocalizations.of(context)?.helloWorld)
                ),
                locale: Locale('ar'),
                supportedLocales: [Locale('en'),Locale('ar')],
                localizationsDelegates: [
                  AppLocalizations.delegate,
                  GlobalMaterialLocalizations.delegate,
                  GlobalCupertinoLocalizations.delegate,
                  GlobalWidgetsLocalizations.delegate,
                ]);
          },
        ));

良好代码:

return MaterialApp(
                home: HomePag(),
                locale: Locale('ar'),
                supportedLocales: [Locale('en'),Locale('ar')],
                localizationsDelegates: [
                  AppLocalizations.delegate,
                  GlobalMaterialLocalizations.delegate,
                  GlobalCupertinoLocalizations.delegate,
                  GlobalWidgetsLocalizations.delegate,
                ]);
          },
        ));


class HomePag extends StatefulWidget {
  const HomePag({Key? key}) : super(key: key);

  @override
  State<HomePag> createState() => _HomePagState();
}

class _HomePagState extends State<HomePag> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          children: [
            Txt(text: AppLocalizations.of(context)?.helloWorld),
            // Txt(text: AppLocalizations.of(context)?.translate('help'))
          ],
        ),
      ),
    );
  }
}
gfttwv5a

gfttwv5a3#

这对我很有帮助。我有一个Cupertino应用程序在MaterielApp之上,这覆盖了MaterielApp和我的应用程序本地化。(内容)为空
https://github.com/flutter/flutter/issues/26365#issuecomment-523536339

jvlzgdj9

jvlzgdj94#

添加l10n.yaml:

nullable-getter: false

相关问题