颤动错误:方法'text'在null上调用。接收者:本地化时为null

时间:2019-10-15 07:20:20

标签: flutter

我正在使用以下代码在Flutter中进行本地化

class LoginFirst1 extends StatefulWidget {
  @override
  _LoginFirstState1 createState() => new _LoginFirstState1();
}

class _LoginFirstState1 extends State<LoginFirst1> {
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  AppTranslationsDelegate _newLocaleDelegate;

  @override
  void initState() {
    super.initState();
    _newLocaleDelegate = AppTranslationsDelegate(newLocale: null);
    application.onLocaleChanged = onLocaleChange;
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: new ThemeData(
        primaryColor: Colors.yellow.shade600,
        primaryColorDark: Colors.yellow.shade300,
        accentColor: Colors.yellow.shade400,
      ),
      debugShowCheckedModeBanner: false,
      home: NavigationDrawerDemo(),
      localizationsDelegates: [
        _newLocaleDelegate,
        //provides localised strings
        GlobalMaterialLocalizations.delegate,
        //provides RTL support
        GlobalWidgetsLocalizations.delegate,
      ],
      supportedLocales: [
        const Locale("en", ""),
        const Locale("uz", ""),
        const Locale("ru", ""),
      ],
    );
  }

  void onLocaleChange(Locale locale) {
    setState(() {
      _newLocaleDelegate = AppTranslationsDelegate(newLocale: locale);
    });
  }
}

之后

class NavigationDrawerDemo extends StatefulWidget {
  //static NavigationDrawerDemo _instance = new NavigationDrawerDemo.internal();

 // NavigationDrawerDemo.internal();

 // factory NavigationDrawerDemo() => _instance;
  static int counter;
  @override
  NavigationDrawerState createState() => new NavigationDrawerState();
 /* @override
  State<StatefulWidget> createState() {
    return new NavigationDrawerState();
  }*/
}

class NavigationDrawerState extends State<NavigationDrawerDemo> {
  int _selectedIndex = 0;
  NetworkUtil _netUtil = new NetworkUtil();
  int index = 0;

  static final List<String> languagesList = application.supportedLanguages;
  static final List<String> languageCodesList =
      application.supportedLanguagesCodes;
  bool pressd = false;
  bool pressd1 = false;
//  String lang ="English";
  final Map<dynamic, dynamic> languagesMap = {
    languagesList[0]: languageCodesList[0],
    languagesList[1]: languageCodesList[1],
    languagesList[2]: languageCodesList[2],
  };

  String label = languagesList[0];
  final _SearchDemoSearchDelegate _delegate = _SearchDemoSearchDelegate();
  int _lastIntegerSelected;
  final formKey = new GlobalKey<FormState>();
  final scaffoldKey = new GlobalKey<ScaffoldState>();
 // BuildContext get context => super.context;
  @override
  void initState() {
    super.initState();
    // AppTranslations.load(Locale('uz'));
    application.onLocaleChanged = onLocaleChange;
    onLocaleChange(Locale(languagesMap["English"]));

  }

  void onLocaleChange(Locale locale) async {
    setState(() {
      AppTranslations.load(locale);
    });
  }


  void _select(String language) {
    print("dd "+language);
    onLocaleChange(Locale(languagesMap[language]));
    setState(() {

    });
  }
  @override
  Widget build(BuildContext context) {
    final l = AppTranslations.of(context);
    List<Widget> drawerOptions = [];
    for (var i = 0; i < drawerItems.length; i++) {
      var d = drawerItems[i];
      drawerOptions.add(new ListTile(
        leading: new Icon(d.icon),
        title: new Text(
          d.title,
          style: new TextStyle(fontSize: 14.0, fontWeight: FontWeight.w400),
        ),
        selected: i == _selectedIndex,
        onTap: () {
          _onSelectItem(i);
          print("_selectedIndex $_selectedIndex");
        },
      ));
    }
    return new Scaffold(
      appBar: AppBar(
        title: Text(l.text("key_log_in")),
        actions: <Widget>[
          IconButton(
            tooltip: 'Search',
            icon: const Icon(Icons.search),
            onPressed: () async {
              final int selected = await showSearch<int>(
                context: context,
                delegate: _delegate,
              );
              if (selected != null && selected != _lastIntegerSelected) {
                setState(() {
                  _lastIntegerSelected = selected;
                });
              }
            },
          ),
          PopupMenuButton<String>(
            // overflow menu
            onSelected: _select,
            icon: new Icon(Icons.language, color: Colors.black),
            itemBuilder: (BuildContext context) {
              return languagesList
                  .map<PopupMenuItem<String>>((String choice) {
                return PopupMenuItem<String>(
                  value: choice,
                  child: Text(choice),
                );
              }).toList();
            },
          ),
        ],
      ),

title: Text(l.text("key_log_in")),上出现此错误

  

在构建NavigationDrawerDemo时引发了以下NoSuchMethodError(脏,依赖项:[_ LocalizationsScope- [GlobalKey#f385b]],状态:NavigationDrawerState#46043):   方法“文本”在null上被调用。   接收者:null   尝试调用:text(“ key_log_in”)

0 个答案:

没有答案