Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振定位-在Null上调用了文本_Flutter_Localization - Fatal编程技术网

Flutter 颤振定位-在Null上调用了文本

Flutter 颤振定位-在Null上调用了文本,flutter,localization,Flutter,Localization,我正在尝试为我的颤振应用程序实现本地化。但是有一个错误,方法“text”在null上被调用。接收者:null尝试呼叫:text(“title”) 问题来自“语言选择器”页面,在该页面中选择语言时会出现错误。标题本应根据语言进行更改。目前有英语和汉语两种语言 import 'package:MyApp/services/AppTranslations.dart'; import 'package:MyApp/services/Application.dart'; import 'package:f

我正在尝试为我的颤振应用程序实现本地化。但是有一个错误,
方法“text”在null上被调用。接收者:null尝试呼叫:text(“title”)

问题来自“语言选择器”页面,在该页面中选择语言时会出现错误。标题本应根据语言进行更改。目前有英语和汉语两种语言

import 'package:MyApp/services/AppTranslations.dart';
import 'package:MyApp/services/Application.dart';
import 'package:flutter/material.dart';


class LanguageSelectorPage extends StatefulWidget {
  @override
  _LanguageSelectorPageState createState() => _LanguageSelectorPageState();
}

class _LanguageSelectorPageState extends State<LanguageSelectorPage> {
  //languagesList also moved to the Application class just like the languageCodesList
  static final List<String> languagesList = application.supportedLanguages;
  static final List<String> languageCodesList =
      application.supportedLanguagesCodes;

  final Map<dynamic, dynamic> languagesMap = {
    languagesList[0]: languageCodesList[0],
    languagesList[1]: languageCodesList[1],
  };

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          AppTranslations.of(context).text("title") //This is where the error come from
        ),
      ),
      body: _buildLanguagesList(),
    );
  }

  _buildLanguagesList() {
    return ListView.builder(
      itemCount: languagesList.length,
      itemBuilder: (context, index) {
        return _buildLanguageItem(languagesList[index]);
      },
    );
  }

  _buildLanguageItem(String language) {
    return InkWell(
      onTap: () {
        print(language);
        application.onLocaleChanged(Locale(languagesMap[language]));
      },
      child: Center(
        child: Padding(
          padding: const EdgeInsets.symmetric(vertical: 20.0),
          child: Text(
            language,
            style: TextStyle(
              fontSize: 24.0,
            ),
          ),
        ),
      ),
    );
  }
}
这是我的应用程序

import 'dart:async';
import 'dart:convert';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show rootBundle;

class AppTranslations {
  Locale locale;
  static Map<dynamic, dynamic> _localisedValues;

  AppTranslations(Locale locale) {
    this.locale = locale;
    _localisedValues = null;
  }

  static AppTranslations of(BuildContext context) {
    return Localizations.of<AppTranslations>(context, AppTranslations);
  }

  static Future<AppTranslations> load(Locale locale) async {
    AppTranslations appTranslations = AppTranslations(locale);
    String jsonContent = await rootBundle.loadString("assets/lang/${locale.languageCode}.json");
    _localisedValues = json.decode(jsonContent);
    return appTranslations;
  }

  get currentLanguage => locale.languageCode;

  String text(String key) {
    return _localisedValues[key] ?? "$key not found";
  }
}
导入'dart:async';
导入“dart:convert”;
导入“dart:ui”;
进口“包装:颤振/材料.省道”;
导入“包:flatter/services.dart”显示根包;
类应用程序翻译{
地点;
静态映射值;
AppTranslations(区域设置){
this.locale=locale;
_LocalizedValues=null;
}
的静态AppTranslations(BuildContext上下文){
返回本地化(上下文、应用程序翻译);
}
静态未来加载(区域设置)异步{
AppTranslations AppTranslations=AppTranslations(语言环境);
String jsonContent=await rootBundle.loadString(“assets/lang/${locale.languageCode}.json”);
_localisedValues=json.decode(jsonContent);
返回appTranslations;
}
获取currentLanguage=>locale.languageCode;
字符串文本(字符串键){
返回_localisedValues[key]?“$key未找到”;
}
}

如果您已经观看了以下视频

那么你肯定会得到一个空错误

我就是这样解决这个问题的

只需更改: 来自此

localeResolutionCallback: (locale, supportedLocales) {
        // Check if the current device locale is supported
        for (var supportedLocale in supportedLocales) {
          if (supportedLocale.languageCode == locale.languageCode &&
              supportedLocale.countryCode == locale.countryCode) {
            return supportedLocale;
          }
        }
        // If the locale of the device is not supported, use the first one
        // from the list (English, in this case).
        return supportedLocales.first;
      },
localeResolutionCallback: (locale, supportedLocales) {
        // Check if the current device locale is supported
        for (var supportedLocale in supportedLocales) {
          if (supportedLocale.languageCode == locale?.languageCode ||
              supportedLocale.countryCode == locale?.countryCode) {
            return supportedLocale;
          }
        }
        // If the locale of the device is not supported, use the first one
        // from the list (English, in this case).
        return supportedLocales.first;
      },
到此

localeResolutionCallback: (locale, supportedLocales) {
        // Check if the current device locale is supported
        for (var supportedLocale in supportedLocales) {
          if (supportedLocale.languageCode == locale.languageCode &&
              supportedLocale.countryCode == locale.countryCode) {
            return supportedLocale;
          }
        }
        // If the locale of the device is not supported, use the first one
        // from the list (English, in this case).
        return supportedLocales.first;
      },
localeResolutionCallback: (locale, supportedLocales) {
        // Check if the current device locale is supported
        for (var supportedLocale in supportedLocales) {
          if (supportedLocale.languageCode == locale?.languageCode ||
              supportedLocale.countryCode == locale?.countryCode) {
            return supportedLocale;
          }
        }
        // If the locale of the device is not supported, use the first one
        // from the list (English, in this case).
        return supportedLocales.first;
      },
它会起作用的