NoSuchMethodError:在 null 上调用了 getter

时间:2021-03-08 10:09:07

标签: flutter localization

        `
    class MainPage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return ChangeNotifierProvider( //Comes from provider class
            create: (context) => LocaleProvider(),
            builder: (context, child) {
              final provider = Provider.of<LocaleProvider>(context);
              return MaterialApp(
                debugShowCheckedModeBanner: false,
                title: 'Forts In Maharashtra',
                locale: provider.locale,
                supportedLocales: L10n.all,
                localizationsDelegates: [
                  AppLocalizations.delegate,
                  GlobalMaterialLocalizations.delegate,
                  GlobalCupertinoLocalizations.delegate,
                  GlobalWidgetsLocalizations.delegate,
                ],
                home: MyApp(),
              );
            });
      }
    }
    
    class MyApp extends StatefulWidget {
      //static const String _title = 'Forts in Maharashtra';
      MyApp({Key key, this.title}) : super(key: key);
      final String title;
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      @override
      void initState() {
        super.initState();
    
        new FirebaseNotifications().setUpFirebase();
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          theme: ThemeData(accentColor: Colors.deepOrange),
          debugShowCheckedModeBanner: false,
          home: Scaffold(
              appBar: AppBar(
                title: Text(
                  AppLocalizations.of(context).apptitle, //Translates the text
                  style: TextStyle(fontSize: 23.0),
                ),
                backgroundColor: Colors.deepOrange,
                actions: [
                  Icon(Icons.language, size: 28),
                  LanguagePick(),
                  const SizedBox(width: 12),
                ],
              ),
              body: MyBottomNavigationBar()),
        );
      }
    }
    
    class MyBottomNavigationBar extends StatefulWidget {
 
    
    class _MyBottomNavigationBarState extends State<MyBottomNavigationBar> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            body: _children[_currentindex],
            bottomNavigationBar: BottomNavigationBar(
              type: BottomNavigationBarType.fixed,
              selectedItemColor: Colors.deepOrange,
              onTap: onTappedBar,
              currentIndex: _currentindex,
              items: [
                BottomNavigationBarItem(
                    icon: Icon(Icons.explore),
                    label: AppLocalizations.of(context).exploreforts), //Giving error here
                BottomNavigationBarItem(
                    icon: Icon(MdiIcons.waves), label: 'Sea Forts'),
                BottomNavigationBarItem(
                    icon: Icon(Icons.terrain), label: 'Attractions'),
                BottomNavigationBarItem(
                    icon: Icon(Icons.location_city), label: 'City Forts'),
              ],
            ));
      }
    }` 

我正在尝试在我的应用程序中使用本地化功能。我可以将 AppBar 标题更改为另一种语言,但无法更改 BottomNavigationBar 上的语言。我在更改 BottomNavigationBar 中的语言时遇到问题。 AppBar 上的 App 语言已更改,但我在 BottomNavigationBar 中使用相同语言时遇到了问题。我在需要的地方添加了评论。

1 个答案:

答案 0 :(得分:0)

这是因为你定义了两个 MaterialApp。只需要在main中定义一个并在整个app中渲染,将_MyAppState类中的MaterialApp去掉