Undefined name 'index'

I’m new to flutter, I try to do a navigator from a listTile in a drawer to a class but I get 3 index errors.

"Undefined name ‘index’

"The values in a const list literal must be constant."

"Invalid constant value."

ListTile(
            leading: Icon(Icons.people),
            title: Text('Associati'),
            onTap: () {
              Navigator.push(context, MaterialPageRoute(builder: (context) => AssociatiPage(index)));
            }
          ),

this is the class

import 'package:flutter/material.dart';
import 'package:flutter_application_orsa/pages/NavBar.dart';

class AssociatiPage extends StatefulWidget {
  const AssociatiPage(index, {super.key});
  
  @override
  State<AssociatiPage> createState() => _AssociatiPageState();
}

class _AssociatiPageState extends State<AssociatiPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: const NavBar(),
      appBar: AppBar(
        backgroundColor: const Color.fromARGB(255, 241, 160, 29),
        leading: Builder(builder: (context) {
          return IconButton(
            onPressed: () {
              Scaffold.of(context).openDrawer();
            },
            icon: const Icon(
              Icons.menu,
            ),
          );
        }),
      ),
      body: const ListTile(
        title: Text('primo'),
        onTap: null,
      )
    );
  }
}

I try

Navigator.push(context, MaterialPageRoute(builder: (context) => AssociatiPage(index)));

>Solution :

Remove const before ListTile, ontap will happen on runtime.

 body: ListTile(
        title: Text('primo'),
        onTap: (){...},
      )

And my assumption is index will come from the ListView.builder, the thing you are trying to do. Or provide 0 to 2 inplace of index.

Leave a Reply