Home > Enterprise >  Flutter error A value of type 'Object?' can't be assigned to a variable of type '
Flutter error A value of type 'Object?' can't be assigned to a variable of type '

Time:06-14

hey I want to make an int variable, but it got an error. please help. the error is the line

"onChanged: (i) => setState(() => value = i),"

actually the last i only, get the red underline, said : A value of type 'Object?' can't be assigned to a variable of type 'int'.

and this is the code :

class Payment extends StatefulWidget {
  @override
  _PaymentState createState() => _PaymentState();
}

class _PaymentState extends State<Payment> {
  int value = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text("SPARK Payment Page"),
        centerTitle: true,
      ),
      body: Column(
        children: [
          HeaderLabel(
            headerText: 'Choose your payment method',
          ),
          Expanded(
            child: ListView.separated(
              itemCount: paymentLabels.length,
              itemBuilder: (context, index) {
                return ListTile(
                  leading: Radio(
                    activeColor: Colors.black,
                    value: index,
                    groupValue: value,
                    onChanged: (i) => setState(() => value = i),
                  ),
                  title: Text(
                    paymentLabels[index],
                    style: TextStyle(color: Colors.blue),
                  ),
                  trailing: Icon(paymentIcons[index], color: Colors.blue),
                );
              },
              separatorBuilder: (context, index) {
                return Divider();
              },
            ),
          ),
          DefaultButton(
              btnText: 'Pay',
              onPressed: () => Navigator.of(context).push(MaterialPageRoute(
                    builder: (context) => Success(),
                  )))
        ],
      ),
    );
  }
}

I've changed the int value = i to String? value ="0"; and I've changed onChanged: (i) => setState(() => value = i as String?)

that make the red underline gone, but when I ran the app, it returns error like this :

_CastError (type 'int' is not a subtype of type 'String?' in type cast)

CodePudding user response:

set your onChanged to:

onChanged: (i) => setState(() => value = i as int?),

or, set the type of the Radio by typing Radio<int?>:

ListTile(
          leading: Radio<int?>(
            activeColor: Colors.black,
            value: index,
            groupValue: value,
            onChanged: (i) => setState(() => value = i),
          ),
          title: Text(
            paymentLabels[index],
            style: TextStyle(color: Colors.blue),
          ),
          trailing: Icon(paymentIcons[index], color: Colors.blue),
        ),

CodePudding user response:

The problem is you the object you have defined is not an int, and you wanted to define it as an int with the value of i, i is an int

you should change the value to int or convert it into an int.

or do this instead set the Radio<int?> if that doesn't work, Radio?

  • Related