繁体   English   中英

Flutter 未为类型“_LeaveApplicationState”定义方法“User”

[英]Flutter The method 'User' isn't defined for the type '_LeaveApplicationState'

我正在为后端使用 Flutter 和 Firebase 构建一个应用程序。 我正在遵循教程指南,但是当我编写代码时遇到两种类型的错误,第一种是; “未为类型‘_LeaveApplicationState’定义方法‘User’”

class LeaveApplication extends StatefulWidget {
  const LeaveApplication({Key? key}) : super(key: key);

  @override
  _LeaveApplicationState createState() => _LeaveApplicationState();
}

class _LeaveApplicationState extends State<LeaveApplication> {
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  final controllerFirstName = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Container(
          margin: EdgeInsets.all(24),
          child: Form(
            key: _formKey,
            child: Column(
              children: <Widget>[
                const Text(
                  'Apply For Work Leave',
                  style: TextStyle(
                    fontWeight: FontWeight.w700,
                    fontSize: 18,
                    color: Colors.black,
                  ),
                ),
                //rest of code....

                  child: MaterialButton(
                    color: Colors.lightBlue,
                    onPressed: () {
                      final user = **User**(
                        fname: controllerFirstName.text,
                        lname: controllerLastName.text,
                        email: controllerEmail.text,
                        phoneno: int.parse(controllerPhoneNo.text),
                        leave: controllerLeaveType,
                        duration: controllerDuration.text,
                      );

                      Navigator.pop(context);

                      createUser(user);

                      if (_formKey.currentState!.validate()) {
                        ScaffoldMessenger.of(context).showSnackBar(
                          const SnackBar(content: Text('Request Sent')),
                        );
                      }
                    },
             
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Future createUser(**User** user) async {
    final docUser = FirebaseFirestore.instance.collection('employees').doc();
    user.id = docUser.id;

    final json = user.toJson();
    await docUser.set(json);
  }
}

第二个错误是“Undefined class 'User'”。 错误出现在粗体/星号区域。 我希望已登录的用户能够输入详细信息并将其发送到 firebase 数据库。 谁能帮忙

如果您正在导入您的用户 model class 这应该可以正常工作,我会建议您将您的用户 class 的名称更改为其他名称,因为 firebase 也有一个用户 class 您可能已经导入了那个用户,请再次检查并让我知道这是否有效

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM