• 로그인
  • 장바구니에 상품이 없습니다.

home2 게시판 Flutter 게시판 Flutter Error: Constructor is marked 'const' so all fields must be final.

Flutter Error: Constructor is marked 'const' so all fields must be final.

2 글 보임 - 1 에서 2 까지 (총 2 중에서)
  • 글쓴이
  • #31331

    김준권
    참가자

    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);
    },
    ),
    ...

     

     

    #31349

    codingapple
    키 마스터

    const MyApp 이렇게 되어있으면

    안에있는 변수는 항상 final a = 1 이렇게 만들어야한다는 에러같군요 

    const를 지우거나 그럽시다 

2 글 보임 - 1 에서 2 까지 (총 2 중에서)
  • 답변은 로그인 후 가능합니다.

About

현재 월 700명 신규수강중입니다.

  (09:00~20:00) 빠른 상담은 카톡 플러스친구 코딩애플 (링크)
  admin@codingapple.com
  이용약관
ⓒ Codingapple, 강의 예제, 영상 복제 금지
top

© Codingapple, All rights reserved. 슈퍼로켓 에듀케이션 / 서울특별시 강동구 고덕로 19길 30 / 사업자등록번호 : 212-26-14752 온라인 교육학원업 / 통신판매업신고번호 : 제 2017-서울강동-0002 호 / 개인정보관리자 : 박종흠