Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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."

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading