无法在Flutter TabBarView中本地化字符串 [英] Cannot localize strings in Flutter TabBarView

查看:75
本文介绍了无法在Flutter TabBarView中本地化字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在TabBarView中本地化字符串时,出现此错误:

When I try to localize a string in a TabBarView I get this error:

NoSuchMethodError:方法"translate"在null上被调用.接收者:null.尝试调用:translate("username).

NoSuchMethodError: The method 'translate' was called on null. Receiver: null. Tried calling: translate("username).

我使用以下代码行将键转换为本地化字符串:

I use this line of code to translate a key to a localized string:

AppLocalizations.of(context).translate('username')

此功能在我的应用程序的所有其他屏幕(除了此屏幕)上都很好用.有谁知道为什么它不起作用以及如何解决?

This works great in all the other screens of my app except this one. Does anyone know why it's not working and how to solve it?

我已经尝试过的一些事情:

Some things I already tried:

  • 传递主屏幕(包含所有TabBarView的屏幕)的上下文

  • pass the context of the main screen (the screen that holds all the TabBarViews)

在构建器中包装每个ListTile

wrap every ListTile in a Builder

将ListView包装在构建器中

wrap the ListView in a Builder

代码:

class AccountScreen extends StatelessWidget {
  final String username;
  final String email;
  final int points;

  AccountScreen(this.username, this.email, this.points);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Padding(
        padding: EdgeInsets.all(10),
        child: ListView(
          children: <Widget>[
            ListTile(
              title: Text(AppLocalizations.of(context).translate('username')),
              subtitle: Text(username),
            ),
            ListTile(
              title:
                  Text(AppLocalizations.of(context).translate('email_address')),
              subtitle: Text(email),
            ),
            ListTile(
              title: Text(AppLocalizations.of(context).translate('points')),
              subtitle: Text('$points'),
            ),
          ],
        ),
      ),
    );
  }
}

推荐答案

问题是应用内有多个MaterialApp小部件,这些小部件引起了有关本地化过程的冲突.删除冗余的MaterialApp小部件可解决此问题.

The problem was that there were multiple MaterialApp widgets inside the app that were causing some conflicts regarding the localisation process. Removing the Redundant MaterialApp widgets fixed the issue.

这篇关于无法在Flutter TabBarView中本地化字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆