-3

I am creating a project where I want to create a list of students and store it and then edit or delete whenever required. I am locally storing data and using sqlite. But while running the project I receive following error: Class 'Future' has no instance getter 'key'. Receiver: Instance of 'Future' Tried calling: key Getting error in if (studentList==null) E/flutter (11424): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: LateInitializationError: Field 'studentList' has not been initialized.

class StudentList extends StatefulWidget {
  final String? streenTitle;

  StudentList(this.streenTitle);
  State<StatefulWidget> createState() {
    return StudentState();
  }
}

class StudentState extends State<StudentList> {
  SQL_Helper? helper = SQL_Helper();
  late List<Student> studentList;
  int connt = 0;

  build(BuildContext context) async {
    **if (studentList==null) {**
      studentList = await <Student>[];
    }

    return Scaffold(
      appBar: AppBar(
        title: Text("Students"),
      ),
      body: getStudentsList(),
      floatingActionButton: FloatingActionButton(onPressed: () {
        NavigatorToStudend("Add New Studend");
      }),
    );
  }

  ListView getStudentsList() {
    return ListView.builder(
        itemCount: connt,
        itemBuilder: (BuildContext context, int pos) {
          return Card(
            color: Colors.red,
            elevation: 2.0,
            child: ListTile(
              leading: CircleAvatar(
                backgroundColor: isPassed(this.studentList[pos].pass),
                child: getIcon(this.studentList[pos].pass),
              ),
              title: Text(this.studentList[pos].name),
              subtitle: Text(this.studentList[pos].description +
                  " | " +
                  this.studentList[pos].date),
              trailing: GestureDetector(
                child: Icon(Icons.delete, color: Colors.green),
                onTap: () {
                  _delete(context, this.studentList[pos]);
                },
              ),
              onTap: () {
                NavigatorToStudend("Edit Sdudent");
              },
            ),
          );
        });
  }

  Color isPassed(int value) {
    switch (value) {
      case 1:
        return Colors.amber;
        break;
      case 2:
        return Color.fromARGB(255, 10, 95, 63);
        break;
    }
    return isPassed(value);
    //// stop in 15:28
  }

  Icon getIcon(int value) {
    switch (value) {
      case 1:
        return Icon(Icons.check);
        break;
      case 2:
        return Icon(Icons.close);
        break;
    }
    return getIcon(value);
  }

  void NavigatorToStudend(String appTitle) {
    Navigator.push(context, MaterialPageRoute(builder: (context) {
      return StudentDetil(appTitle);
    }));
  }
}

void _delete(BuildContext context, Student student) async {
  

  int result = await SQL_Helper.helper!.deleteStudent(student.id);
  if (result != 0) {
    _showSenchBar(context, " Student is been deleted");

    //listview
  }
}

void _showSenchBar(BuildContext context, String mag) async {
  final snackBar = await SnackBar(content: Text(mag));
  Scaffold.of(context).showSnackBar(snackBar);
}

//void NavigatorToStudend(String s) {}

1 Answer 1

0

There's a few issues with your code. For starters: late List<Student> studentList; isn't nullable, so checking if (studentList==null) { will always be false. There's a few typos and code errors too (such as your build method not having an @override decorator. I highly suggest using an IDE (like Android Studio) if you aren't already.

If studentList is always going to be awaiting something, I suggest using a FutureBuilder

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.