【问题标题】:When I used to validator: , it is given error message on emilator screen : type 'String' is not a subtype of type '((String?) => String?)?'当我使用验证器:时,在模拟器屏幕上给出错误消息:类型“字符串”不是类型“((字符串?)=>字符串?)?”的子类型
【发布时间】:2022-01-30 23:11:23
【问题描述】:

我的模拟器屏幕上有一个按钮“Yeni Öğrenci(新学生)”,当我按下它时,我在新的模拟器页面上看到错误屏幕:类型“字符串”不是类型“((字符串?) => 字符串?)?'。我的验证器是否有错误:或 Navigator.push 代码?我在 SOF 中尝试了类似的解决方案,但我无法解决这个问题。 Android Studio 也没有显示任何问题,但是当我在模拟器上按下新页面的按钮时,这是在 Android Studio 中编写的:

以下 _TypeError 被抛出构建

StudentAdd(脏,状态:_StudentAddState#935ee):

type 'String' 不是 type '((String?) => String?)?' 的子类型

相关的导致错误的小部件是:StudentAdd StudentAdd:file:///C:/ornekler/temel_winget%20feedback/lib/main.dart:103:81

我的意思是这个语法: Navigator.push(context, MaterialPageRoute(builder: (context)=>StudentAdd(students)));

或者这个:

validator: validateLastName(''),

另一个按钮和页面上的相同错误: Navigator.push(context, MaterialPageRoute(builder(context)=>StudentEdit(selectedStudent)));

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);
    }
  }
}

我在这个包'student_add.dart'中调用StudentAdd:

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;
}
}

【问题讨论】:

  • 你的学生班是什么样的?
  • 我最后为您的问题编辑了代码。对你有用吗?

标签: flutter dart


【解决方案1】:

您的构造函数不需要显式赋值。他们甚至不需要身体。改变

Student(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);

this. 告诉 Dart 将参数分配给类属性。

【讨论】:

  • 好吧,我改了,Android Studio没问题;但模拟器错误仍然存​​在。
  • 问题可能来自您的验证功能。你能发一个吗?
  • 我回去查看代码上次工作的时间,我看到当我编写此代码时 --validator: validateFirstName(''),-- emilator 在屏幕上显示错误:'String' is不是 '((String?) => String?)?' 类型的子类型
  • 你见过this answer吗?这可能是相关的。
  • 我明天会尝试解决这个问题,我感谢您的关注。
猜你喜欢
  • 2019-11-21
  • 1970-01-01
  • 2020-09-10
  • 2019-02-27
  • 2020-10-28
  • 1970-01-01
  • 2021-12-20
  • 2021-08-09
  • 2021-11-26
相关资源
最近更新 更多