Home > Blockchain >  A value of type 'Null' can't be assigned to a parameter of type 'int' in a
A value of type 'Null' can't be assigned to a parameter of type 'int' in a

Time:10-06

I am trying to pass the index, which I get from a widget constructor to another page. But it won't work, because my created variable (index) in my constructor basically has no value. I get that.. but I don't know how to solve it. Any advice?

class GC extends StatefulWidget {
const GC({Key? key, required this.index}) : super(key: key);
final int index;

@override
State<GC> createState() => _GCState();
}

class _GCState extends State<GC> {
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 10.0),
      child: GestureDetector(
        onTap: () {
          Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => const DetailViewGC(
              name: "Test", 
              amount: "25€", 
              image: AssetImage("lib/Assets/Images/test.png"), 
              index: widget.index,
            ),
          ));
        },
        onLongPress: () {
          showDialog<String>(
            context: context,
            builder: (BuildContext context) => AlertDialog(
              title: const Text('Delete'),
              content:
                  const Text('Do you really want to delete?'),
              actions: <Widget>[
                TextButton(
                  onPressed: () => Navigator.pop(context, 'OK'),
                  child: const Text('Cancel'),
                ),
                TextButton(
                  onPressed: () {
                    Navigator.pop(context, 'OK');
                  },
                  child: const Text(
                    'Delete',
                    style: TextStyle(fontWeight: FontWeight.bold),
                  ),
                )
              ],
            ),
          );
        },
        child: const GCHero(),
      ),
    );
  }
}

CodePudding user response:

You cannot use const keyword in a non-const context

Just remove the const keyword:

MaterialPageRoute(builder: (context) => /* const */ DetailViewGC(
    name: "Test", 
    amount: "25€", 
    image: AssetImage("lib/Assets/Images/test.png"), 
    index: widget.index, // [widget.index] This is not a constant value, we cannot use it inside a const context
  ),
));
  • Related