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

home2 게시판 Flutter 게시판 range error

range error

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

    오승준
    참가자
    import 'package:flutter/material.dart';
    void main() {
      runApp( MaterialApp(
          home:MyApp()
      )
      );
    }
    class MyApp extends StatefulWidget {
      MyApp({super.key});
      @override
      State<MyApp> createState() => _MyAppState();
    }
    class _MyAppState extends State<MyApp> {
      var total = 3;
      var num = '완료!';
      var a = 1;
      var name=['정유성', '김민서', '김도연'];
      var like=[0,0,0];
      addName(){
        setState(() {
          name.add('말숙');
        });
      }
      addOne(){
        setState((){
          total++;
        });
      }
      @override
      Widget build(BuildContext context) {
        return Scaffold(
              appBar: AppBar(title : Text(total.toString()),),
              floatingActionButton: FloatingActionButton(
                child: Text(a.toString()),
                onPressed: (){
                  showDialog(
                    context: context,
                    builder: (context){
                      return DialogUI(addOne : addOne, addName: addName);
             });
                },),
              body: ListView.builder(
                itemCount: name.length,
                itemBuilder: (context, i){
                  return  ListTile(
                    leading: Text(like[i].toString()),
                    title: Text(name[i]),
                    trailing: ElevatedButton(child: Text('좋아요'), onPressed: (){
                      setState(() {
                        like[i]++;
                      });
                    },),
                  );
                    
                },
              ),
              bottomNavigationBar: BottomAppBar(),
            );
    }
    }
    
    
    class DialogUI extends StatelessWidget {
      DialogUI({super.key, this.addOne, this.addName});
      final addOne;
      final addName;
      var inputData = TextEditingController();
      @override
      Widget build(BuildContext context) {
        return AlertDialog(
          title: Text('Friend'),
          content: SingleChildScrollView(
            child: TextField(
              controller: inputData,
              decoration: InputDecoration(hintText: '친구이름'),
            ),
          ),
          actions: [
            TextButton(onPressed: (){addOne(); addName();}, child: Text('완료')),
            TextButton(onPressed: (){Navigator.pop(context);}, child: Text('취소'))
          ],
        );
      }
    }
    
    
    말숙을 추가시키려고 완료버튼을 누르면
    RangeError(index): Index out of range: index should be less than 3:3
    에러가 뜹니다.
    
    무엇이 문제일까요ㅜ
    #93239

    codingapple
    키 마스터
    이름하나 추가하면 like도 0 하나 추가합시다
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 호 / 개인정보관리자 : 박종흠