flutter - NoSuchMethodError:在null上调用了 setter/getter 'languageCode'.Receiver:null尝试调用:languageCode

标签 flutter dart sharedpreferences

单击单选按钮后,我构建了一个用于更改语言的应用程序,但是当更改语言的页面打开时,它显示红色屏幕错误,并显示以下标题。

该错误是:-NoSuchMethodError:在null上调用了getter'languageCode'.Receiver:null尝试调用:languageCode

这里是我的代码:- [如果您需要更多代码,请通知我]

   SettingsPage extends StatefulWidget {   final data;   final method;

  SettingsPage({this.data,this.method});

  @override   _SettingsPageState createState() =>
_SettingsPageState(data: this.data,method: method);

}

class _SettingsPageState extends State<SettingsPage> {   var data;   int method;

  PrayerMethods selectedMethod = PrayerMethods.Umm_Al_Qura_University_Makkah;   Languages radioLang;


  _SettingsPageState({this.data,this.method}){
    if(method!=null){
      selectedMethod = PrayerMethods.values[method];
      print("starting setting page: selectdMethod: [$method]: [${selectedMethod.toString()}]");
    }   }

  @override   void initState() {
    print("initState: data.savedLocale.languageCode: ${data.savedLocale.languageCode}");
    if(data.savedLocale.languageCode == "ar"){
      radioLang = Languages.ar;
    }else{
      radioLang = Languages.en;
    }
    print("initState: radioLand: $radioLang");
    super.initState();   }



  @override   Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        title: new Text(AppLocalizations.of(context).tr("Settings")),
        backgroundColor: Color(0xFF614729),
        elevation: 10,
      ),
      body: Container(
        child: ListView(
          children: <Widget>[
            Column(
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                Container(
                  padding: EdgeInsets.all(15.0),
                  child: Text(AppLocalizations.of(context).tr("Language"),
                          style: TextStyle(fontSize: 21,fontWeight: FontWeight.bold),),
                ),
                Column(
                  children: <Widget>[
                    RadioListTile(
                      value: Languages.en,
                      groupValue: radioLang,
                      activeColor: Colors.black,
                      onChanged: (val){
                        setState(() {
                          radioLang = val;
                        });
                      },
                      title: Text("English",),),
                    RadioListTile(
                      value: Languages.ar,
                      groupValue: radioLang,
                      activeColor: Colors.black,
                      onChanged: (val){
                        setState(() {
                          radioLang = val;
                        });
                      },
                      title: Text(AppLocalizations.of(context).tr("Arabic"),
                        ),),
                  ],
                ),
                Divider(
                  height: 50,
                  color: Colors.brown,
                ),
                Container(
                  padding: EdgeInsets.all(15.0),
                  child: Text(AppLocalizations.of(context).tr("Prayer Methods"),
                    style: TextStyle(fontSize: 20,fontWeight: FontWeight.bold),),
                ),
                Column(
                  children: <Widget>[
                RadioListTile(value: PrayerMethods.Umm_Al_Qura_University_Makkah, groupValue: selectedMethod,
                    activeColor: Colors.black,
                    title: Text(AppLocalizations.of(context).tr("Umm Al-Qura University, Makkah", )),
                    onChanged: (val){
                      setState(() {
                        selectedMethod = val;
                      });
                    }),
                    RadioListTile(value: PrayerMethods.Muslim_WorldLeague, groupValue: selectedMethod,
                        activeColor: Colors.black,
                        title: Text(AppLocalizations.of(context).tr("Muslim World League"),
                        ),
                        onChanged: (val){
                          setState(() {
                            selectedMethod = val;
                          });
                        }),
                    RadioListTile(value: PrayerMethods.Gulf_Region, groupValue: selectedMethod,
                        activeColor: Colors.black,
                        title: Text(AppLocalizations.of(context).tr("Gulf Region"),
                        ),
                        onChanged: (val){
                          setState(() {
                            selectedMethod = val;
                          });
                        }),
                    RadioListTile(value: PrayerMethods.Kuwait, groupValue: selectedMethod,
                        activeColor: Colors.black,
                        title: Text(AppLocalizations.of(context).tr("Kuwait"),
                        ),
                        onChanged: (val){
                          setState(() {
                            selectedMethod = val;
                          });
                        }),
                    RadioListTile(value: PrayerMethods.Qatar, groupValue: selectedMethod,
                        activeColor: Colors.black,
                        title: Text(AppLocalizations.of(context).tr("Qatar"),
                        ),
                        onChanged: (val){
                          setState(() {
                            selectedMethod = val;
                          });
                        }),
                    RadioListTile(value: PrayerMethods.University_of_Islamic_Sciences_Karachi, groupValue: selectedMethod,
                        activeColor: Colors.black,
                        title: Text(AppLocalizations.of(context).tr("University of Islamic Sciences, Karachi",)),
                        onChanged: (val){
                          setState(() {
                            selectedMethod = val;
                          });
                        }),
                    RadioListTile(value: PrayerMethods.Islamic_Society_of_North_America, groupValue: selectedMethod,
                        activeColor: Colors.black,
                        title: Text(AppLocalizations.of(context).tr("Islamic Society of North America"),
                          ),
                        onChanged: (val){
                          setState(() {
                            selectedMethod = val;
                          });
                        }),
                    RadioListTile(value: PrayerMethods.Egyptian_GeneralAuthority_of_Survey, groupValue: selectedMethod,
                        activeColor: Colors.black,
                        title: Text(AppLocalizations.of(context).tr("Egyptian General Authority of Survey"),
                        ),
                        onChanged: (val){
                          setState(() {
                            selectedMethod = val;
                          });
                        }),
                    RadioListTile(value: PrayerMethods.Shia_Ithna_Ansari, groupValue: selectedMethod,
                        activeColor: Colors.black,
                        title: Text(AppLocalizations.of(context).tr("Ithna-Ashari"),
                        ),
                        onChanged: (val){
                          setState(() {
                            selectedMethod = val;
                          });
                        }),
                    RadioListTile(value: PrayerMethods.Institute_ofGeophysics_University_of_Tehran, groupValue: selectedMethod,
                        activeColor: Colors.black,
                        title: Text(AppLocalizations.of(context).tr("Institute of Geophysics, University of Tehran"),
                        ),
                        onChanged: (val){
                          setState(() {
                            selectedMethod = val;
                          });
                        }),
                    RadioListTile(value: PrayerMethods.MajlisUgama_Islam_Singapura_Singapore, groupValue: selectedMethod,
                        activeColor: Colors.black,
                        title: Text(AppLocalizations.of(context).tr("Majlis Ugama Islam Singapura, Singapore"),
                        ),
                        onChanged: (val){
                          setState(() {
                            selectedMethod = val;
                          });
                        }),
                    RadioListTile(value: PrayerMethods.UnionOrganization_islamic_de_France, groupValue: selectedMethod,
                        activeColor: Colors.black,
                        title: Text(AppLocalizations.of(context).tr("Union Organization islamic de France"),
                          ),
                        onChanged: (val){
                          setState(() {
                            selectedMethod = val;
                          });
                        }),
                    RadioListTile(value: PrayerMethods.DiyanetIsleri_Baskanligi_Turkey, groupValue: selectedMethod,
                        activeColor: Colors.black,
                        title: Text(AppLocalizations.of(context).tr("Diyanet İşleri Başkanlığı, Turkey"),
                        ),
                        onChanged: (val){
                          setState(() {
                            selectedMethod = val;
                          });
                        }),
                    RadioListTile(value: PrayerMethods.SpiritualAdministration_of_Muslims_of_Russia, groupValue: selectedMethod,
                        activeColor: Colors.black,
                        title: Text(AppLocalizations.of(context).tr("Spiritual Administration of Muslims of Russia"),
                        ),
                        onChanged: (val){
                          setState(() {
                            selectedMethod = val;
                        });
                        }),
                  ],
                ),
              ],
            ),
          ],

        ),
      ),

      floatingActionButton: Container(
        width: 100.0,
        height: 50.0,
        child: FloatingActionButton(onPressed: () async{
          _onLoading();
          if(selectedMethod.index != method){
            await updateLanguageAndMethod();
          }
          Navigator.pop(context);
          changeLanguage(data,radioLang.index);
          Navigator.pushReplacement(context, MaterialPageRoute(builder: (BuildContext context) => MainScreen()));
        },
        child: new Text(AppLocalizations.of(context).tr("Save"),style: TextStyle(fontSize: 15.0),),
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(14)),
          elevation: 10,
          backgroundColor: Color(0xFF614729),
        ),
      ),
    );   }   void changeLanguage(var data,int value){
    this.setState(() {
      switch (value) {
        case 0:
          updateLanguageInSharedPrefs(value);
          data.changeLocale(Locale("en"));
          print("changeLanguage(): EN selected");
          break;
        case 1:
          updateLanguageInSharedPrefs(value);
          data.changeLocale(Locale("ar"));
          print("changeLanguage(): AR selected");
          break;
      }
    });   }   void updateLanguageInSharedPrefs(int lang) async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setInt('language', lang);   }

  dynamic getLanguageInSharedPrefs() async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.get('language');   }

  Future<String> updateMethodInSharedPrefs(int method) async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setInt('prayer_method', method);
    String value = "updateMethod: $method";
    print(value);
    return value;

  }

  void updateLanguageAndMethod() async{
    await updateMethodInSharedPrefs(selectedMethod.index);
    MonthlyPrayerData monthlyPrayerData = await updatePrayerDataFileUsingAPI(method: selectedMethod.index);
    print("updateLanguageAndMethod completed.");

  }

  void _onLoading() {
    showDialog(
      builder: (_) => Center(child: CircularProgressIndicator()),
      context: context,
      barrierDismissible: false,
    );   }

}

最佳答案

从错误中我们可以说data.savedLocale为null。我们无法提供所提供的大量信息。

也许提供更多的代码。

关于flutter - NoSuchMethodError:在null上调用了 setter/getter 'languageCode'.Receiver:null尝试调用:languageCode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57697382/

相关文章:

Dart : Printing integer along with string

dart - Flutter/Dart 和 AWS 开发工具包

dart - 错误未定义类 TimeOfDay。尝试使用 flutter 时间时

android - If-else 语句根据先前的 Activity 选择获取共享首选项

android - Kotlin:如何在 SharedPreferences 中保存数组列表

Android - 启动然后在第一次打开时下载

firebase - 无法在两者的最新版本上使用 firebase 编译 flutter 应用程序

flutter :检查列表中是否已存在对象

arrays - 在API中使用用户输入来按用户加载数据时发现异常

flutter - Flutter/Dart- 'List<dynamic>'不是 'List<Product>'类型的子类型