Home > Back-end >  1 positional argument(s) expected, but 0 found. Try adding the missing arguments error
1 positional argument(s) expected, but 0 found. Try adding the missing arguments error

Time:08-09

I am getting this positional arguments error. I am supposed to provide an argument in that parentheses but I cannot seem to know how. 

class SwitchScreen extends StatefulWidget {
  @override
  SwitchClass createState() => SwitchClass(); // **THE ERROR IS HERE...**
}

class SwitchClass extends State {
  bool isSwitched;

  SwitchClass(this.isSwitched);
  @override
  Widget build(BuildContext context) {
    return Switch(
      value: isSwitched,
      activeColor: Colors.blue,
      onChanged: (value) {
        setState(() {
          isSwitched = value;
        });
      },
    );
  }
}

CodePudding user response:

it needs the state of the widget you are creating to make changes. rewrite it to the following and it will work

class SwitchClass extends StatefulWidget {
  const SwitchClass({Key? key}) : super(key: key);

  @override
  State<SwitchClass> createState() => _SwitchClassState();
}

class _SwitchClassState extends State<SwitchClass> {
  bool isSwitched = false;

  @override
  Widget build(BuildContext context) {
    return Switch(
      value: isSwitched,
      activeColor: Colors.blue,
      onChanged: (value) {
        setState(() {
          isSwitched = value;
        });
      },
    );
  }
}
  • Related