在另一页的异步匿名函数中使用初始化的String

问题描述

您如何在该匿名函数之外的其他任何地方使用String theFlag

我不得不将其设为匿名函数,因为我无法将单独的异步函数指向onpressed中的RaisedButton,因为它仅与onChanged一起使用,现在我只需要传递{ {1}}到另一个屏幕上的另一个小部件

请帮助和介意初学者的问题

String theFlag

解决方法

您可以这样做,

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

我该怎么用?

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

//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),