SQFLITE数据库飘动
我一直在尝试为我的应用程序创建用于注册/登录的数据库,但是我没有这样做,我的问题是当我尝试单击注册时,什么也不会发生。这是我的数据库代码。至于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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当没有验证错误时,form.validate()返回true。
只需更改!form.validate()form.validate()
form.validate() return true when there are no validation errors.
Just change !form.validate() to form.validate()