1

I have a class

class Consultant {
  final int id;
  final String consultantFirstName;
  final String consultantLastName;
  final String consultantNickName;

  const Consultant({
    this.id,
    this.consultantFirstName,
    this.consultantLastName,
    this.consultantNickName,

  });
}

final Consultant marco = Consultant(
    id: 1,
    consultantFirstName: 'Marco',
    consultantLastName: 'Marcello',
    
);
final Consultant carmela = Consultant(
    id: 2,
    consultantFirstName: 'Carmela',
    consultantLastName: 'Maiocchi',
    consultantNickName: 'Mariz Safe',


List<Consultant> consultant = [
  marco,
  carmela,

];

If I call this list into ListviewBuilder all is fine because of INDEX into a Listviewbuilder, the problem is if I am out of ListViewbuilder I cant get the index and I get error because there is not Index if I call consultant: consultant[index]; to pass data to a new widget here is how I call into Listview.builder and I get value:

ListView.builder(
                padding: EdgeInsets.all(5),
                itemCount: consultant.length,
                itemBuilder: (BuildContext context, int index) {
                  return Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      GestureDetector(
                        onTap: () => Navigator.push(
                          context,
                          MaterialPageRoute(
                            builder: (_) => ConsultantProfile(
                              consultant: consultant[index],
                            ),
                          ),
                        ),

but if I go out from Listview.builder widget and I try to call another widget passing the data as this:

appBar: AppBar(
          title: Text(
            'Desteeno',
            style: TextStyle(fontWeight: FontWeight.w300, color: Colors.white),
          ),
          backgroundColor: Colors.purple,
          centerTitle: true,
          elevation: 15,
          shadowColor: Colors.black,
          actions: [
            IconButton(
              onPressed: () {
                Navigator.pop(context);
                showDialog(
                  context: context,
                  builder: (context) => ContactForm(
                    consultant: consultant[index],
                  ),
                );
              },

I get index red underlined and error: The getter 'index' isn't defined for the class '_ConsultantProfileState' I understand I need to define the index from my List but I dont know how

1 Answers1

1

Try this , it should help:

appBar: AppBar(
          title: Text(
            'Desteeno',
            style: TextStyle(fontWeight: FontWeight.w300, color: Colors.white),
          ),
          backgroundColor: Colors.purple,
          centerTitle: true,
          elevation: 15,
          shadowColor: Colors.black,
          actions: [
            IconButton(
              onPressed: () {
                Navigator.pop(context);
                showDialog(
                  context: context,
                  builder: (context) => ContactForm(
                    consultant: widget.consultant,
                  ),
                );
Akshit Ostwal
  • 451
  • 3
  • 14