Listview.builder 강의에서
Stateless widget 안에 var a = 1 만들고 버튼누르면서 increment하는코드가 에러뜹니다.
lib/main.dart:8:9: Error: Constructor is marked 'const' so all fields must be final.
const MyApp({Key? key}) : super(key: key);
^
lib/main.dart:10:7: Context: Field isn't final, but constructor is 'const'.
var a = 1;
플러터버젼은 Flutter 2.10.4 입니다.
왜그러는걸까요?
코드는 다음과같습니다.
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
var a = 1;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
floatingActionButton: FloatingActionButton(
child: Text('Click'),
onPressed: () {
a++;
print(a);
},
),
...