SQFLITE数据库飘动

发布于 2025-02-08 04:09:21 字数 2712 浏览 3 评论 0原文

我一直在尝试为我的应用程序创建用于注册/登录的数据库,但是我没有这样做,我的问题是当我尝试单击注册时,什么也不会发生。这是我的数据库代码。至于SQL,我非常了解它,因为我曾经与MySQL一起工作,但是对于Flutter和Sqflite,我仍在学习它,非常感谢您是否可以帮助我,谢谢。

 class DbHelper {
        
    
      static const String DB_Name = 'test.db';
      static const String Table_User = 'user';
      static const int Version = 1;
      static const String _Email = 'email';
      static const String _Password = 'password';
      static const String _Name = 'name';
      
      static Database? _db;
      Future<Database?> get db async {
        if (_db != null) {
          return _db;
        }
        _db = await initDb();

        return _db;
    
      }
    
    initDb ()  async {
      io.Directory documentsDirectory = await getApplicationDocumentsDirectory();
      String path = join (documentsDirectory.path, DB_Name);
      var db = await openDatabase(path , version: Version, onCreate: _onCreate);
      return db;
    }
    
    _onCreate (Database db, int intVersion) async {
      await db.execute("CREATE TABLE $Table_User ("
        "$_Email TEXT ,"
        "$_Password TEXT ," 
        "$_Name TEXT ,"
        " PRIMARY KEY ($_Email)"
        
        " )");
    
    }
    
    Future<int> saveData(UserModel user) async{
      var dbClient = await db;
      var res = await dbClient!.insert(Table_User, user.toMap());
      return res;
    }
    

    
    
       Future<UserModel?> getLoginUser(String email, String password) async {
        var dbClient = await db;
        var res = await dbClient!.rawQuery("SELECT * FROM $Table_User WHERE "
            "$_Email = '$email' AND "
            "$_Password = '$password'");
    
        if (res.length > 0) {
          return UserModel.fromMap(res.first);
        }
    
        return null;
      }
    
    }

这是我的注册功能

signUp() async {
    final form = _formKey.currentState;
    String uEmail = _Email.text;
     String password = _Password.text;
      String cpassword = _CPassword.text;
       String uname = _Name.text;

      if(form != null && !form.validate ()){

        if (password != cpassword) {
          alertDialog(context, "Password Mismatch");

        }
        else{
          _formKey.currentState?.save();

          UserModel uModel = UserModel(uEmail, password, uname);
          await dbHelper.insert(uModel).then((userData) {
          alertDialog(context, "Success");

          Navigator.push(context, MaterialPageRoute(builder: (_) => LoginForm()));
          
          }).catchError((error){
            print(error);
            alertDialog(context, "Error: Data Save Fail");
          });
          

        }
        
      }

  }

i've been trying to create database for signup/login for my app but i failed to do so , my problem is when i try to click register, nothing happen. This is my db code. As for sql i understand it really well because i use to work with mySQL but for flutter and sqflite I still in the process to learn it , really appreciate if you can help me , thank you.

 class DbHelper {
        
    
      static const String DB_Name = 'test.db';
      static const String Table_User = 'user';
      static const int Version = 1;
      static const String _Email = 'email';
      static const String _Password = 'password';
      static const String _Name = 'name';
      
      static Database? _db;
      Future<Database?> get db async {
        if (_db != null) {
          return _db;
        }
        _db = await initDb();

        return _db;
    
      }
    
    initDb ()  async {
      io.Directory documentsDirectory = await getApplicationDocumentsDirectory();
      String path = join (documentsDirectory.path, DB_Name);
      var db = await openDatabase(path , version: Version, onCreate: _onCreate);
      return db;
    }
    
    _onCreate (Database db, int intVersion) async {
      await db.execute("CREATE TABLE $Table_User ("
        "$_Email TEXT ,"
        "$_Password TEXT ," 
        "$_Name TEXT ,"
        " PRIMARY KEY ($_Email)"
        
        " )");
    
    }
    
    Future<int> saveData(UserModel user) async{
      var dbClient = await db;
      var res = await dbClient!.insert(Table_User, user.toMap());
      return res;
    }
    

    
    
       Future<UserModel?> getLoginUser(String email, String password) async {
        var dbClient = await db;
        var res = await dbClient!.rawQuery("SELECT * FROM $Table_User WHERE "
            "$_Email = '$email' AND "
            "$_Password = '$password'");
    
        if (res.length > 0) {
          return UserModel.fromMap(res.first);
        }
    
        return null;
      }
    
    }

and this is my sign up function

signUp() async {
    final form = _formKey.currentState;
    String uEmail = _Email.text;
     String password = _Password.text;
      String cpassword = _CPassword.text;
       String uname = _Name.text;

      if(form != null && !form.validate ()){

        if (password != cpassword) {
          alertDialog(context, "Password Mismatch");

        }
        else{
          _formKey.currentState?.save();

          UserModel uModel = UserModel(uEmail, password, uname);
          await dbHelper.insert(uModel).then((userData) {
          alertDialog(context, "Success");

          Navigator.push(context, MaterialPageRoute(builder: (_) => LoginForm()));
          
          }).catchError((error){
            print(error);
            alertDialog(context, "Error: Data Save Fail");
          });
          

        }
        
      }

  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

停滞 2025-02-15 04:09:21

当没有验证错误时,form.validate()返回true。

只需更改!form.validate()form.validate()

form.validate() return true when there are no validation errors.

Just change !form.validate() to form.validate()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文