Home > Software engineering >  problem while creating checkbox on flutter
problem while creating checkbox on flutter

Time:07-05

I have followed from the information provided, but this code still has an error (setState() is not detected, there is a red underline). Can someone help me in this case?

bool fpass = false;
CheckboxListTile(
                  title: Text("Show Password"),
                  value: fpass,
                  onChanged: (value) {
                    setState(() {
                      fpass = value!;
                    });
                  })

CodePudding user response:

Use below code :

import 'package:flutter/material.dart';

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

  @override
  _CheckBoxDemoState createState() => _CheckBoxDemoState();
}

class _CheckBoxDemoState extends State<CheckBoxDemo> {
  bool fpass = false;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: [

        CheckboxListTile(
        title: Text("Show Password"),
          value: fpass,
          onChanged: (value) {
            setState(() {
              fpass = value!;
            });
          })
          ],
        ),
      ),
    );
  }
}

CodePudding user response:

setState only works with statefulwidget. Are you using statelesswidget ?

  • Related