Home > Net >  set state in the on change
set state in the on change

Time:10-29

import 'package:flutter/material.dart';

bool jov = false;

class boox extends StatelessWidget {
  const boox({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      drawer: Drawer(),
      body: Container(
        child: Column(children: [
          Row(
            children: [
              Text("jordn"),
              Checkbox(
                  value: jov,
                  onChanged: (String val) {
                    setstate(() {
                      jov = val;
                    });
                  })
            ],
          ),
          Row(
            children: [Text("data")],
          )
        ]),
      ),
    );
  }
}

The setstate cannot appear on onchanged and cant take it

I am difintion the variables and the problem still appear

and try to write the code and still appear this problem if any can fix him to me or no way

CodePudding user response:

You need to use StatefulWidget to use setState and onChanged will provide nullable bool for Checkbox it will be onChanged: (bool? value) {

class boox extends StatefulWidget {
  const boox({super.key});

  @override
  State<boox> createState() => _booxState();
}

class _booxState extends State<boox> {
  bool jov = false;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      drawer: Drawer(),
      body: Container(
        child: Column(children: [
          Row(
            children: [
              Text("jordn"),
              Checkbox(
                value: jov,
                onChanged: (bool? value) {
                  setState(() {
                    jov = value??false;
                  });
                },
              )
            ],
          ),
          Row(
            children: [Text("data")],
          )
        ]),
      ),
    );
  }
}

More about StatefulWidget

CodePudding user response:

Your example wouldn't work

boox is Stateless widget and doesn't have any setState method In your case you can see changes your app should update a tree node which belong boox widget

You have to use Stateful widget instead

  • Related