fplopez
fplopez

Reputation: 1

I'm having trouble trying to use setState to update a string variable in Flutter

Folks, this is driving mu nuts. It was supposed a no brainer called to setState to change the text of variable cupon_data. I try many solutions I found researching but I cant make it work. Please help.

Text(
                      cupon_data,
                      style: Theme.of(context)
                          .textTheme
                          .subtitle1!
                          .copyWith(fontWeight: FontWeight.w600),
                    ),
                    const SizedBox(width: 10),
                    TextButton(
                      style: ButtonStyle(
                        foregroundColor:
                            MaterialStateProperty.all<Color>(Colors.blue),
                      ),
                      onPressed: () {
                        setState(() {
                          cupon_data = 'Copied';
                        });
                      },
                      child: Text('TextButton'),
                    )

The variable cupon_data is defined on the top of the buid method:

Widget build(BuildContext context) {
    String cupon_data = widget.coupon.coupon_code;

Any help is appreciated.

Upvotes: 0

Views: 51

Answers (2)

Theodore MCA
Theodore MCA

Reputation: 1178

class AnyPage extends StatefulWidget {
  const AnyPage({Key? key, required this.coupon}) : super(key: key);
  final Widget coupon;
  @override
  State<AnyPage> createState() => _AnyPageState();
}
class _AnyPageState extends State<AnyPage> {
  
  String cupon_data = widget.coupon.coupon_code;
  @override
  Widget build(BuildContext context) {
 //  any Declaration here will rerun when you call setstate..
 //  here is a wrong place to put  String cupon_data = widget.coupon.coupon_code;
    return Scaffold(
      body: Text(
                      cupon_data,
                      style: Theme.of(context)
                          .textTheme
                          .subtitle1!
                          .copyWith(fontWeight: FontWeight.w600),
                    ),
                    const SizedBox(width: 10),
                    TextButton(
                      style: ButtonStyle(
                        foregroundColor:
                            MaterialStateProperty.all<Color>(Colors.blue),
                      ),
                      onPressed: () {
                        setState(() {
                          cupon_data = 'Copied';
                        });
                      },
                      child: Text('TextButton'),
                    )

    );
  }
}

Upvotes: 0

Ananda Pramono
Ananda Pramono

Reputation: 1009

You can't update a variable inside build using setState, because setState rebuild your widget (basically, it will call build), so it will get widget.coupon.coupon_code everytime and override you input. Try to declare coupon_data outside of build.

class _ExampleState extends State<Example> {
  String cupon_data = widget.coupon.coupon_code;

  @override
  Widget build(BuildContext context) {
    
  }
}

Upvotes: 2

Related Questions