'When I used to validator: , it is given error message on emilator screen : type 'String' is not a subtype of type '((String?) => String?)?'

I have a button on my emilator screen 'Yeni Öğrenci(New Student)', when I push it, I see error screen on new emilator page : type 'String' is not a subtype of type '((String?) => String?)?'. Is there an error my validator: or Navigator.push code? I tried similar solution in SOF, but I couldn't fix this. Android Studio also shows no problems, but when I push button for new page on emilator, that is writing in Android Studio :

The following _TypeError was thrown building

StudentAdd(dirty, state: _StudentAddState#935ee):

type 'String' is not a subtype of type '((String?) => String?)?'

The relevant error-causing widget was: StudentAdd StudentAdd:file:///C:/ornekler/temel_winget%20feedback/lib/main.dart:103:81

I mean this syntax : Navigator.push(context, MaterialPageRoute(builder: (context)=>StudentAdd(students)));

or this :

validator: validateLastName(''),

Same error on another button and page : Navigator.push(context, MaterialPageRoute(builder(context)=>StudentEdit(selectedStudent)));

All code in main.dart :

import 'package:flutter/material.dart';
import 'package:temel_winget/models/student.dart';
import 'package:temel_winget/screens/student_add.dart';
import 'package:temel_winget/screens/student_edit.dart';

void main() {
  runApp(MaterialApp(home: MyApp()));
}

class MyApp extends StatefulWidget {
  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String mesaj = 'Minik Ailem';
  Student selectedStudent=Student.withId(0, '', '', 0);

  List<Student> students = [
    Student.withId(1,'İlkay', 'Keskin', 97),
    Student.withId(2,'Mustafa', 'Keskin', 65),
    Student.withId(3,'Burak', 'Keskin', 87),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(mesaj),
      ),
      body: buildBody(context),
    );
  }

  void mesajGoster(BuildContext context, String mesaj) {
    var alert = AlertDialog(
      title: Text('İşlem Sonucu'),
      content: Text(mesaj),
    );

    showDialog(context: context, builder: (BuildContext) => alert);
  }

  Widget buildBody(BuildContext context) {
    return Column(
      children: [
        Expanded(
            child: ListView.builder(
                itemCount: students.length,
                itemBuilder: (BuildContext context, int index) {
                  return ListTile(
                    leading: CircleAvatar(
                      backgroundImage: AssetImage('dosyalar/images/pp.jpg'),
                    ),
                    title: Text(students[index].firstName +
                        ' ' +
                        students[index].lastName),
                    subtitle: Text('Sınavdan Aldığı Not : ' +
                        students[index].grade.toString() +
                        ' [' +
                        students[index].getStatus +
                        ']'),
                    trailing: buildStatusIcon(students[index].grade),
                    onTap: () {
                      setState(() {
                        selectedStudent = students[index];
                      });

                      print(selectedStudent.firstName);
                    },
                  );
                })),
        Text('Seçilmiş Aile Üyesi : ' + selectedStudent.firstName),
        Row(
          children: [
            Flexible(
              fit: FlexFit.tight,
              flex: 1,
              child: ElevatedButton(
                style: ElevatedButton.styleFrom(
                  primary: Colors.amberAccent,
                  //onPrimary: Colors.amberAccent,
                ),
                child: Row(
                  children: <Widget>[
                    Icon(Icons.add),
                    Text('Yeni Öğrenci'),
                  ],
                ),
                onPressed: () {

                  Navigator.push(context, MaterialPageRoute(builder: (context)=>StudentAdd(students)));

                },
              ),
            ),
            Flexible(
              fit: FlexFit.tight,
              flex: 1,
              child: ElevatedButton(
                style: ElevatedButton.styleFrom(
                  primary: Colors.blueAccent,
                  //onPrimary: Colors.amberAccent,
                ),
                child: Row(
                  children: <Widget>[
                    Icon(Icons.update),
                    SizedBox(
                      width: 5.0,
                    ),
                    Text('Güncelle'),
                  ],
                ),
                onPressed: () {
                  Navigator.push(context, MaterialPageRoute(builder: (context)=>StudentEdit(selectedStudent)));
                },
              ),
            ),
            Flexible(
              fit: FlexFit.tight,
              flex: 1,
              child: ElevatedButton(
                style: ElevatedButton.styleFrom(
                 primary: Colors.amberAccent,
                  //onPrimary: Colors.amberAccent,
                ),

                child: Row(
                  children: <Widget>[
                    Icon(Icons.delete),
                    SizedBox(width: 5.0,),
                    Text('Sil'),
                  ],
                ),
                onPressed: () {
                  setState(() {
                    students.remove(selectedStudent);
                  });

                  var mesaj = 'Silindi : ' + selectedStudent.firstName;
                  mesajGoster(context, mesaj);
                },
              ),
            ),
          ],
        )
      ],
    );
  }

  Widget buildStatusIcon(int grade) {
    if (grade >= 50) {
      return Icon(Icons.done);
    } else if (grade >= 40) {
      return Icon(Icons.album);
    } else {
      return Icon(Icons.clear);
    }
  }
}

I call StudentAdd in this package 'student_add.dart':

import 'package:flutter/material.dart';
import 'package:temel_winget/models/student.dart';
import 'package:temel_winget/validation/student_validator.dart';


class StudentAdd extends StatefulWidget {

  final List<Student> students;
  StudentAdd(this.students){
  }
  @override
  State<StatefulWidget> createState() {
    return _StudentAddState(students);
  }
}

class _StudentAddState extends State with StudentValidationMixin {
  late List<Student> students;
  var student = Student.without(0, '', '', 0);
  var formKey = GlobalKey<FormState>();

  _StudentAddState(List<Student> students){
    this.students = students;
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text('Yeni Öğrenci Ekle'),
      ),
      body: Container(
        margin: EdgeInsets.all(20.0),
        //margin: EdgeInsets.only(top: 20.0, right: 20.0, left: 20.0),
        child: Form(
          key: formKey,
          child: Column(
            children: <Widget>[
              buildFirstNameField(),
              buildLastNameField(),
              buildGradeField(),
              buildSubmitButton(),
            ],
          ),
        ),
      ),
    );
  }

  buildFirstNameField() {
    return TextFormField(
      decoration: InputDecoration(
          labelText: 'Öğrenci Adı', hintText: 'İlkay'),
      validator: validateFirstName(''),
      onSaved: (value) {
        student.firstName = '';
      },
    );
  }
  buildLastNameField() {
    return TextFormField(
      decoration: InputDecoration(
          labelText: 'Öğrenci Soyadı', hintText: 'Keskin'),
      validator: validateLastName(''),
      onSaved: (value) {
        student.lastName = '';
      },
    );
  }
  buildGradeField() {
    return TextFormField(
      decoration: InputDecoration(
          labelText: 'Aldığı Not', hintText: '65'),
      validator: validateGrade(''),
      onSaved: (value) {
        student.grade = int.parse('');
      },
    );
  }

  buildSubmitButton() {
    return ElevatedButton(
      child: Text('Kaydet'),
      onPressed: (){
        if(formKey.currentState!.validate()){
          formKey.currentState?.save();
          students.add(student);
          saveStudent();
          Navigator.pop(context);
        }
      },
    );
  }

  void saveStudent() {
    print(student.firstName);
    print(student.lastName);
    print(student.grade);
  }
}

student.dart :

class Student {
  int id;
  String firstName;
  String lastName;
  int grade;

  Student.withId(this.id,this.firstName, this.lastName, this.grade) {
    id = id ;
    firstName = firstName;
    lastName = lastName;
    grade = grade;
  }

  Student(this.id, this.firstName, this.lastName, this.grade) {
    //id = id;
    firstName = firstName;
    lastName = lastName;
    grade = grade;
  }

  Student.withInfo(this.id,this.firstName, this.lastName, this.grade) {
    id = id ;
    firstName = firstName;
    lastName = lastName;
    grade = grade;
  }

  Student.without(this.id,this.firstName, this.lastName, this.grade) {
    id = id ;
    firstName = firstName;
    lastName = lastName;
    grade = grade;
  }

  String get getfirstName{
    return 'OGR - ' + this.firstName;
  }

  void set setfirstName(String value){
    this.firstName = value;
  }
  String get getStatus{
    String message = '';
    if (this.grade >= 50) {
      message = 'Geçti';
    } else if (this.grade >= 40) {
      message = 'Bütünlemeye Kaldı';
    } else {
      message = 'Kaldı';
    }
    return message;
}
}


Solution 1:[1]

Your constructors do not need the explicit assignments. They don't even need a body. Change

Student(this.id, this.firstName, this.lastName, this.grade) {
    id = id;
    firstName = firstName;
    lastName = lastName;
    grade = grade;
}

to

Student(this.id, this.firstName, this.lastName, this.grade);

The this. tells Dart to assign the arguments to the class properties.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stack Underflow