I come from a python-background trying to learn java-like languages. This is written in dart for use with flutter.
I get an error when I try to run the following.
class _MyGameState extends State<MyGame> {
int _numberOfTeams = 4;
List<int> _teamPoints = List.filled(_numberOfTeams, 0);
The error message I get is:
The instance member '_numberOfTeams' can't be accessed in an initializer. Try replacing the reference to the instance member with a different expression
Why is that, and how does one avoid it? (Obviously, this example is simplified and i could easily simply omit the variable _numberOfTeams
and circumvent the problem, but that's not the point.)
(There are a lot of very similiar questions on here. All the answers to those questions offers ways to cicrumvent the specific problem, but not "why" the problem occurs or how to think when writing in languages where this is common. A few relevant search words that I could look into, or a link to a guide/tutorial/article would be very appreciated :)