I'm getting this error: Too few positional arguments: 5 required, 0 given

Hi all. I’m learning flutter. I created another folder called page. In this folder i created a new class called Home. Now i was trying to create a circular button and when i press this button it shows three new buttons. I created a new class called CircularButton and 5 parameters in your constructor(width, height, color, icon and onClick function). When i call from my _HomeState class i get this error:

Too few positional arguments: 5 required, 0 given.

Below my complete code

import ‘package:flutter/material.dart’;
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}

class _HomeState extends State {
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Scaffold(
body: Container(
height: size.height,
width: size.width,
child: Stack(
children: [
Positioned(
right: 30,
bottom: 30,
child: Stack(
children: [
CircularButton(
width: 60,
height: 60,
color: Colors.red,
icon: Icon(
Icons.menu,
color: Colors.white,
),
onClick: (){

              },
            ),
          ],
        ))
      ]
    )
  ),
);

}
}

class CircularButton extends StatelessWidget {
final double width;
final double height;
final Color color;
final Icon icon;
final Function onClick;

CircularButton(this.width, this.height, this.color, this.icon, this.onClick);
@override
Widget build(BuildContext context){
return Container(
decoration: BoxDecoration(color: color, shape: BoxShape.circle),
width: width,
height: height,
child: IconButton(icon: icon, enableFeedback: true, onPressed: onClick,),
);
}
}

In these lines i get the error
CircularButton(

              width: 60,
              height: 60,
              color: Colors.red,
              icon: Icon(
                Icons.menu,
                color: Colors.white,
              ),
              onClick: (){

              },

How can i solve this error?

Inside the CircularButton class, it’s properties (variables) inside the constructor are defined as positional arguments.

CircularButton(this.width, this.height, this.color, this.icon, this.onClick);

When this(CircularButton) Widget is called inside Home class, instead of positional argument, named arguments are used.

CircularButton(
  width: 60,
  height: 60,
  color: Colors.red,
  icon: Icon(
    Icons.menu,
    color: Colors.white,
  ),
  onClick: (){

  },
),

Hence the error “Too few positional arguments: 5 requied, 0 given”
As per CircularButton class definition it is expecting positional arguments whenever an object of this is created.

Given that you are using named arguments when creating an object CircularButton inside Home class, you can modify the constructor of CircularButton class to accept named arguments.
This can be achieved by wrapping the argument list of the constructor with a pair of {}

CircularButton({this.width, this.height, this.color, this.icon, this.onClick});
1 Like