可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have something like this. I am having difficulty understanding this error. Why does accessing filterController
here give this error here ? but it doesnt give this error if I move the current entire TextFormField
creation (between comments A and B) inside the build method ? How does moving the entire TextFormField
inside the build method make filterController
static then and resolve this issue ?
class AppHomeState extends State<AppHome> with SingleTickerProviderStateMixin { TabController _tabController; final filterController = new TextEditingController(text: "Search"); //----A TextFormField email = new TextFormField( keyboardType: TextInputType.emailAddress, controller: filterController, ------>ERROR : Error: Only static members can be accessed in initializers ); //----B @override Widget build(BuildContext context) { return new Scaffold( appBar: new AppBar(..), ); } }
How can I resolve this issue ?
回答1:
class AppHomeState extends State<AppHome> with SingleTickerProviderStateMixin { TabController _tabController; final filterController = new TextEditingController(text: "Search"); TextFormField email = ...
...
is an initializer and there is no way to access this
at this point. Initializers are executed before the constructor, but this
is only allowed to be accessed after the call to the super constructor (implicit in your example) was completed. Therefore only in the constructor body (or later) access to this
is allowed.
This is why you get the error message:
controller: filterController,
accesses this.filterController
(this
is implicit if you don't write it explicit).
To work around your issue (assuming email
needs to be final
) you can use a factory constructor and a constructor initializer list:
class AppHomeState extends State<AppHome> with SingleTickerProviderStateMixin { factory SingleTickerProviderStateMixin() => new SingleTickerProviderStateMixin._(new TextEditingController(text: "Search")); SingleTickerProviderStateMixin._(TextEditingController textEditingController) : this.filterController = textEditingController, this.email = new TextFormField( keyboardType: TextInputType.emailAddress, controller: textEditingController); TabController _tabController; final filterController; final TextFormField email;
or when the email
field does not need to be final email
can be initialized in the constructor initializer list:
class AppHomeState extends State<AppHome> with SingleTickerProviderStateMixin { SingleTickerProviderStateMixin() { email = new TextFormField( keyboardType: TextInputType.emailAddress, controller: filterController, ); } TabController _tabController; final filterController = new TextEditingController(text: "Search"); TextFormField email;
but in Flutter widgets initState
is usually used for that
class AppHomeState extends State<AppHome> with SingleTickerProviderStateMixin { @override void initState() { super.initState(); email = new TextFormField( keyboardType: TextInputType.emailAddress, controller: filterController, ); } TabController _tabController; final filterController = new TextEditingController(text: "Search"); TextFormField email;
回答2:
You can convert this variable to a function and you can take context in this function parameters.
Example
Widget myDialog (BuildContext context) { return new Scaffold( backgroundColor: Colors.white, body: new Center( child: new Column( children: <Widget>[ new Text("Invalid Username/Password"), new Text("Please verify your login credentials"), new RaisedButton( child: new Text("Ok"), onPressed:() { Navigator.pop(context);//Error : Only static members can be accessed in initializers } ), ], ), ) ); } // Using if you are doing in a class this.myDialog(context); // Using if you are using a global function myDialog(context);
But, i think you want to show a error message. So, you can do it with a dialog not an page. It's more efficient because you can specify your dialog box with buttons or messages and you can use this error dialog everywhere. Let's look my global helper function for showing error messages.
void showError(BuildContext context, String error) { showSnackBar( context, new Text( 'Error', style: new TextStyle(color: Theme.of(context).errorColor), ), content: new SingleChildScrollView( child: new Text(error) ), actions: <Widget>[ new FlatButton( child: new Text( 'Ok', style: new TextStyle( color: Colors.white ), ), onPressed: () { Navigator.of(context).pop(); }, color: Theme.of(context).errorColor, ), ] ); } // Using in everywhere showError(context, 'Sample Error');