繁体   English   中英

Sqlite 数据库颤动

[英]Sqflite Database flutter

我一直在尝试为我的应用程序创建注册/登录数据库,但我没有这样做,我的问题是当我尝试点击注册时,什么也没有发生。 这是我的数据库代码。 至于 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");
          });
          

        }
        
      }

  }

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

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

暂无
暂无

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

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