Home > Blockchain >  Flutter - The following ArgumentError was thrown while handling a gesture: Invalid argument(s)
Flutter - The following ArgumentError was thrown while handling a gesture: Invalid argument(s)

Time:07-29

I am a little bit puzzled because of this error. I am copying the code below so you can see what I am trying to achieve.

I have a Stateless widget. It call a widget. To this widget, I am passing several information. One of them is an int named index.

I want to update a list so I know what "day" has been selected by the user.

index is supposed to help me to know which record to update in the list.

But, ontap=> generate the error mentioned in the title. I do not understand why? Please, can you advise? Many thanks.

class WeekDayV2 extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                WidgetButtonDay(
                  text: daysOfTheWeek[0].substring(0,3),
                  onPressed: ()  {
                    },
                  index: 0,
                    ),
                WidgetButtonDay(
                  text: daysOfTheWeek[1].substring(0,3),
                  onPressed: () => print('Click'),
                  index: 1,
                ),
                WidgetButtonDay(
                  text: daysOfTheWeek[2].substring(0,3),
                  onPressed: () => print('Click'),
                  index: 2,
                ),
                WidgetButtonDay(
                  text: daysOfTheWeek[3].substring(0,3),
                  onPressed: () => print('Click'),
                  index: 3,
                ),
                WidgetButtonDay(
                  text: daysOfTheWeek[4].substring(0,3),
                  onPressed: () => print('Click'),
                  index: 4,
                ),
                WidgetButtonDay(
                  text: daysOfTheWeek[5].substring(0,3),
                  onPressed: () => print('Click'),
                  index: 5,
                ),
                WidgetButtonDay(
                  text: daysOfTheWeek[6].substring(0,3),
                  onPressed: () => print('Click'),
                  index: 6,
                ),
              ],
    );
  }
}
class WidgetButtonDay extends StatefulWidget {
   WidgetButtonDay({
    Key key,
     this.text,
     this.onPressed,
     this.index,
  }) : super(key: key);

  @override
  State<WidgetButtonDay> createState() => _WidgetButtonDayState();

  final String text;
  final VoidCallback onPressed;
  int index;
}

class _WidgetButtonDayState extends State<WidgetButtonDay> {
  bool isSelected = false;
  int index;
  List testSelectedDays = [false,false,false,false,false,false,false,];
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 30,
      width: 50,
        decoration:isSelected? BoxDecoration(
          color: Colors.blue,
          borderRadius: BorderRadius.all(Radius.circular(15.0)),
          border: Border.all(color: Colors.black)):BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(Radius.circular(15.0)),
            border: Border.all(color: Colors.black)),
      child: Center(
        child:InkWell(
            child: Text(widget.text),

        onTap: (){
          testSelectedDays[index] = isSelected;
            setState(() => isSelected = !isSelected);
            widget.onPressed();

            print (testSelectedDays);
            print (index);
            print ('index');
      },),
      ),
    );
  }
}

CodePudding user response:

On your _WidgetButtonDayState index never assign value.

class _WidgetButtonDayState extends State<WidgetButtonDay> {
  bool isSelected = false;
  int index; //this

To get widget index do

  int index = widget.index; //this

Or

onTap: (){
      testSelectedDays[widget.index] = isSelected;

Also I would think better upgrading the project to null-safety.

  • Related