在另一个页面的异步匿名函数中使用初始化的字符串

Using initialized String in asynchronous anonymous function in another page

你怎么能在这个匿名函数之外的任何地方使用 String theFlag

我不得不把它变成一个匿名函数,因为我没能在 RaisedButton 中将一个单独的异步函数指向 onPressed 因为它只适用于 onChanged 现在我只需要将 String theFlag 传递给另一个屏幕中的另一个小部件

请各位帮忙介意初学者的问题

  onPressed:() async {
       Locale _temp = await setLocale(LanguageClass.languageList()[index].langCode);
       BldrsApp.setLocale(context, _temp);
    
       String theFlag = LanguageClass.languageList()[index].langFlag ; //I need to use this elsewhere
            print(theFlag);
   },

你可以这样做,

String iCanBeAcessed; //make this variable somewhere else, so that you can use it anywhere.
//Inside your anonymous function
 iCanBeAcessed=theFlag;

我该如何使用它?

您可以在 class 之外声明它,并且可以在您的项目中的任何地方使用它。 例如,

//imports

String iCanBeAcessed;

class yourClassName{
//All your other functions and build method

现在,按以下方式导入 dart 文件(您在其中声明了 iCanBeAcessed)。

import dartFile.dart as flagString;

//Now, you can use this variable anywhere!

Text(flagString.iCanBeAcessed),