D
D
Donut1232022-02-02 12:17:34
Dart
Donut123, 2022-02-02 12:17:34

Flutter. Error Can't access 'this' in a field initializer to read 'a'. var c = a + 1; ^?

Hello connoisseurs. Please help me figure out why it is not possible to declare a variable c using another variable. Here is my code.

class why extends StatefulWidget {
@override
_whyState createState() => _whyState();
}
class _whyState extends State {

int a = 1;
var c = a + 1;

@override
Widget build(BuildContext context) {
return Container();
}
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Neonoviiwolf, 2022-02-02
@Neonoviiwolf

of course it won’t work, it’s not js or python (it seems to work there), your variables are at the same level, either take out “a” as global, or initialize “c” in the function

U
Umpiro, 2022-02-03
@Umpiro

late var c = a + 1;
https://dart.dev/guides/language/language-tour#lat...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question