Hello Guys, how are you all? Hope you all are fine. You might face Error: Only static members can be accessed in initializers as this error in a flutter. here I come with all possible solutions for this error.
we are providing you all possible solutions to solve this error. let’s start this article without wasting your time.
How Error: Only static members can be accessed in initializers This Error Occurs ?
This is my Entire Code.
final filterController = new TextEditingController(text: "Search");
//---- Point 1
TextFormField email = new TextFormField(
keyboardType: TextInputType.emailAddress,
controller: filterController, ------>This Line Give Me Error
);
//---- Point 2
Here Is Error that through in my terminal.
ERROR : Error: Only static members can be accessed in initializers
How to solve Error: Only static member can accessed in initializers ?
- How to solve Error: Only static member can accessed in initializers ?
to solve Error: Only static member can accessed in initializers Problem is caused due to 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 allowed.
- Error: Only static member can accessed in initializers
to solve Error: Only static member can accessed in initializers Problem is caused due to 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 allowed.
Solution 1
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.
You can use this method
Widget getEmailController(){
return new TextFormField(
keyboardType: TextInputType.emailAddress,
controller: filterController,
);
}
Use In UI
body: Container(
child: getEmailController();
)
Solution 2
You can do it this way: First declared and after initialised inside your didChangeDependencies() method, like this
Declared your variable
List<Tab> tabsList = [];
initialised tabsList
tabsList = [
Tab(text: getTranslated(context, "tab_1")),
Tab(text: getTranslated(context, "tab_2")),
Tab(text: getTranslated(context, "tab_3"))
];
Full codes Example
class _MyClassState extends State<MyClass>
with TickerProviderStateMixin<MyClass> {
TabController tabController;
List<Tab> tabsList = [];
@override
void didChangeDependencies() {
tabsList = [
Tab(text: getTranslated(context, "tab_1")),
Tab(text: getTranslated(context, "tab_2")),
Tab(text: getTranslated(context, "tab_3"))
];
tabController =
TabController(length: tabsList.length, vsync: this, initialIndex: 0);
super.didChangeDependencies();
}
}
Solution 3
I faced the exact same issue, and I was able to tackle the problem by setting the initial value of the TextFormField by adding the value I need to the controller’s text, example:
_carPlateController.text = _initValues['carPlate'];
Or
filterController.text = 'search';
I hope this helps!
Summery
So it’s all About All possible solutions. Hope this above all solution helped you a lot. Comment below Your thoughts and your queries. Comment Below on your suggestion.
Check Out Below Article
- The argument type ‘String’ can’t be assigned to the parameter type ‘int’
- How to use Conditional statement in widget in flutter?
- How to use Hexadecimal HEX Color Code String in Flutter Dart ?
- Unhandled Exception: Bad state: Insecure HTTP is not allowed by platform in a flutter
- How to Add Borders to a Widget In Flutter ?
Leave a Reply