Flutter 如何在Flatter中使用SQFLite检查登录应用程序的用户凭据
在注册页面中,我尝试检查保存到表中的用户名和密码。如果表中存在用户名和密码,则处理用户导航并导航到其他屏幕用户 但当我尝试访问表并使用此方法检查用户名和密码时:Flutter 如何在Flatter中使用SQFLite检查登录应用程序的用户凭据,flutter,dynamic,future,sqflite,Flutter,Dynamic,Future,Sqflite,在注册页面中,我尝试检查保存到表中的用户名和密码。如果表中存在用户名和密码,则处理用户导航并导航到其他屏幕用户 但当我尝试访问表并使用此方法检查用户名和密码时: Future<dynamic> checkLogin(String userName, String password) async { final dbClient = await db; return await dbClient.query(USER_TABLE, where: "$
Future<dynamic> checkLogin(String userName, String password) async {
final dbClient = await db;
return await dbClient.query(USER_TABLE,
where: "$USERNAME = ? AND $PASSWORD = ?",
whereArgs: [userName, password],
limit: 1);
}
Future checkLogin(字符串用户名、字符串密码)异步{
最终数据库客户端=等待数据库;
return wait dbClient.query(用户表,
其中:“$USERNAME=?和$PASSWORD=?”,
wherergs:[用户名、密码],
限额:1),;
}
我得到了这个错误:
引发了另一个异常:“Future”类型不是“String”类型的子类型。
请指导我如何更改此方法以获取用户名和密码,然后根据结果处理用户导航?
谢谢你的帮助
编辑:
登录\u screen.dart:
import 'package:atlas_gen_demo/screens/register_screen.dart';
import 'package:flutter/material.dart';
import 'package:atlas_gen_demo/Animation/FadeAnimation.dart';
import 'package:flushbar/flushbar.dart';
import 'package:atlas_gen_demo/data/storage/db_helper.dart';
class LoginScreen extends StatelessWidget {
static const routeName = '/login';
var dbHelper;
final usernameController = TextEditingController();
final passwordController = TextEditingController();
void navigateToRegister(BuildContext ctx) {
Navigator.of(ctx).pushNamed(
RegisterScreen.routeName,
);
}
validate(BuildContext ctx) {
if (usernameController.text != "" && passwordController.text != "") {
dbHelper = DBHelper();
var test =
dbHelper.checkLogin(usernameController.text, passwordController.text);
// just for test what we got here
showFlushBar(ctx, "test", test);
//navigateToUsersList(ctx);
} else {
showFlushBar(ctx, "خطا", "اطلاعات را وارد نمایید");
}
}
void navigateToUsersList(BuildContext ctx) {
Navigator.of(ctx).pushNamed(
LoginScreen.routeName,
);
}
void showFlushBar(BuildContext context, String title, String text) {
Flushbar(
padding: EdgeInsets.all(10),
borderRadius: 8,
backgroundGradient: LinearGradient(
colors: [Colors.purple.shade800, Colors.purpleAccent.shade700],
stops: [0.6, 1],
),
boxShadows: [
BoxShadow(
color: Colors.black,
offset: Offset(3, 3),
blurRadius: 3,
)
],
dismissDirection: FlushbarDismissDirection.HORIZONTAL,
forwardAnimationCurve: Curves.fastLinearToSlowEaseIn,
titleText: Text(
title,
style: TextStyle(fontFamily: 'mainBold', color: Colors.white),
),
messageText: Text(
text,
style: TextStyle(fontFamily: 'mainMedium', color: Colors.white),
),
duration: Duration(seconds: 3),
).show(context);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: SingleChildScrollView(
child: Container(
child: Column(
children: <Widget>[
Container(
height: 250,
margin: EdgeInsets.only(top: 50),
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/login.png'),
fit: BoxFit.fill,
),
),
),
Positioned(
child: FadeAnimation(
1.8,
Container(
margin: EdgeInsets.only(top: 10),
child: Center(
child: Text(
"ورود به برنامه",
textAlign: TextAlign.center,
style: TextStyle(
color: Color.fromRGBO(143, 148, 251, 1),
fontSize: 30,
fontWeight: FontWeight.bold,
fontFamily: 'persianBold',
),
),
),
)),
),
Padding(
padding: EdgeInsets.all(30.0),
child: Column(
children: <Widget>[
Container(
padding: EdgeInsets.all(5),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(10),
boxShadow: [
BoxShadow(
color: Color.fromRGBO(143, 148, 251, .2),
blurRadius: 20.0,
offset: Offset(0, 10))
],
),
child: Column(
children: <Widget>[
Container(
padding: EdgeInsets.all(8.0),
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(color: Colors.grey[100]),
)),
child: TextFormField(
controller: usernameController,
textDirection: TextDirection.rtl,
textAlign: TextAlign.right,
decoration: InputDecoration(
border: InputBorder.none,
hintText: "نام کاربری",
hintStyle: TextStyle(
color: Colors.grey[400],
fontFamily: 'persianMedium',
fontSize: 14,
),
),
),
),
Container(
padding: EdgeInsets.all(8.0),
child: TextFormField(
controller: passwordController,
textAlign: TextAlign.right,
textDirection: TextDirection.rtl,
decoration: InputDecoration(
border: InputBorder.none,
hintText: "کلمه عبور",
hintStyle: TextStyle(
color: Colors.grey[400],
fontFamily: 'persianMedium',
fontSize: 14,
),
),
),
),
],
),
),
SizedBox(
height: 30,
),
FadeAnimation(
2,
InkWell(
onTap: () => validate(context),
child: Container(
height: 50,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
gradient: LinearGradient(
colors: [
Color.fromRGBO(143, 148, 251, .4),
Color.fromRGBO(143, 148, 251, .8),
],
),
),
child: Center(
child: Text(
"ورود به برنامه",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontFamily: 'persianBold',
fontSize: 18,
),
),
),
),
)),
SizedBox(
height: 40,
),
FadeAnimation(
1.5,
InkWell(
onTap: () => navigateToRegister(context),
child: Text(
"ثبت نام در برنامه",
style: TextStyle(
fontFamily: 'persianMedium',
fontSize: 14,
color: Color.fromRGBO(143, 148, 251, .6),
),
),
)),
],
),
)
],
),
),
),
);
}
}
import'包:atlas_gen_demo/screens/register_screen.dart';
进口“包装:颤振/材料.省道”;
导入“包:atlas_gen_demo/Animation/FadeAnimation.dart”;
导入“包:flushbar/flushbar.dart”;
导入“包:atlas_gen_demo/data/storage/db_helper.dart”;
类LoginScreen扩展了无状态小部件{
静态常量routeName='/login';
var-dbHelper;
最终用户名控制器=TextEditingController();
最终密码控制器=TextEditingController();
无效导航寄存器(BuildContext ctx){
Navigator.of(ctx.pushName)(
RegisterScreen.routeName,
);
}
验证(BuildContext ctx){
if(usernameController.text!=“”&passwordController.text!=“”){
dbHelper=dbHelper();
var检验=
dbHelper.checkLogin(usernameController.text、passwordController.text);
//只是为了测试我们在这里得到了什么
显示冲洗条(ctx,“测试”,测试);
//导航用户列表(ctx);
}否则{
showFlushBar(ctx、ctx、ctx、ctx);
}
}
无效navigateToUsersList(BuildContext ctx){
Navigator.of(ctx.pushName)(
LoginScreen.routeName,
);
}
void showFlushBar(构建上下文上下文、字符串标题、字符串文本){
冲水杆(
填充:边缘设置。全部(10),
边界半径:8,
背景梯度:线性梯度(
颜色:[colors.purple.shade800,colors.purpleacent.shade700],
停止:[0.6,1],
),
箱形阴影:[
箱形阴影(
颜色:颜色,黑色,
偏移量:偏移量(3,3),
半径:3,
)
],
dismissDirection:FlushbarDismissDirection.HORIZONTAL,
forwardAnimationCurve:Curves.FastLinearToSloweasin,
标题文本:文本(
标题
样式:TextStyle(fontFamily:'mainBold',颜色:Colors.white),
),
messageText:Text(
文本,
样式:TextStyle(fontFamily:'mainMedium',颜色:Colors.white),
),
持续时间:持续时间(秒数:3),
).show(上下文);
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
背景颜色:Colors.white,
正文:SingleChildScrollView(
子:容器(
子:列(
儿童:[
容器(
身高:250,
页边距:仅限边缘集(顶部:50),
装饰:盒子装饰(
图像:装饰图像(
image:AssetImage('assets/images/login.png'),
fit:BoxFit.fill,
),
),
),
定位(
孩子:FadeAnimation(
1.8,
容器(
页边空白:仅限边集(前10页),
儿童:中心(
子:文本(
"ورود به برنامه",
textAlign:textAlign.center,
样式:TextStyle(
颜色:颜色。来自RGBO(143148251,1),
尺寸:30,
fontWeight:fontWeight.bold,
fontFamily:“波斯博尔德”,
),
),
),
)),
),
填充物(
填充:所有边缘设置(30.0),
子:列(
儿童:[
容器(
填充:边缘设置。全部(5),
装饰:盒子装饰(
颜色:颜色,白色,
边界半径:边界半径。圆形(10),
boxShadow:[
箱形阴影(
颜色:颜色。来自RGBO(143148251,.2),
半径:20.0,
偏移量:偏移量(0,10))
],
),
子:列(
儿童:[
容器(
填充:边缘设置。全部(8.0),
装饰:盒子装饰(
边界:边界(
底部:边框边(颜色:Colors.grey[100]),
)),
子项:TextFormField(
控制器:usernameController,
textDirection:textDirection.rtl,
textAlign:textAlign.right,
装饰:输入装饰(
边框:InputBorder.none,
hintText:“نامکابی”,
hintStyle:TextStyle(
颜色:颜色。灰色[400],
fontFamily:“波斯媒体”,
import 'dart:async';
import 'dart:io' as io;
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path_provider/path_provider.dart';
import '../../models/user.dart';
class DBHelper {
static Database _db;
static const String ID = 'id';
static const String NAME = 'name';
static const String FAMILY = 'family';
static const String USERNAME = 'username';
static const String PASSWORD = 'password';
static const String BIRTHDAY = 'birthday';
static const String MOBILE = 'mobile';
static const String NATIONAL_ID = 'nationalId';
static const String USER_TABLE = 'User';
static const String DB_NAME = 'user.db';
Future<Database> get db async {
if (_db != null) {
return _db;
}
_db = await initDb();
return _db;
}
initDb() async {
io.Directory documentDirectory = await getApplicationDocumentsDirectory();
String path = join(documentDirectory.path, DB_NAME);
var db = await openDatabase(path, version: 1, onCreate: _onCreate);
return db;
}
_onCreate(Database db, int version) async {
await db.execute(
"CREATE TABLE $USER_TABLE ($ID INTEGER PRIMARY KEY, $NAME TEXT, $FAMILY TEXT, $USERNAME TEXT NOT NULL, $PASSWORD TEXT , $BIRTHDAY TEXT, $MOBILE TEXT, $NATIONAL_ID TEXT UNIQUE)");
}
Future<User> save(User user) async {
var dbClient = await db;
user.id = await dbClient.insert(USER_TABLE, user.toMap());
return user;
}
Future<List<User>> getUsers() async {
var dbClient = await db;
List<Map> maps = await dbClient.query(USER_TABLE, columns: [
ID,
NAME,
FAMILY,
USERNAME,
PASSWORD,
BIRTHDAY,
MOBILE,
NATIONAL_ID
]);
List<User> users = [];
if (maps.length > 0) {
for (int i = 0; i < maps.length; i++) {
users.add(User.fromMap(maps[i]));
}
}
return users;
}
Future<dynamic> getUser(String nationalId) async {
final dbClient = await db;
return await dbClient.query(USER_TABLE,
where: "$NATIONAL_ID = ?", whereArgs: [nationalId], limit: 1);
}
Future<User> checkLogin(String userName, String password) async {
final dbClient = await db;
var res = await dbClient.rawQuery(
"SELECT * FROM $USER_TABLE WHERE username = '$userName' and password = '$password'");
if (res.length > 0) {
return new User.fromMap(res.first);
}
return null;
}
Future<int> delete(String nationalId) async {
var dbClient = await db;
return await dbClient
.delete(USER_TABLE, where: '$nationalId = ?', whereArgs: [nationalId]);
}
Future<int> update(User user) async {
var dbClient = await db;
return await dbClient.update(USER_TABLE, user.toMap(),
where: '$NATIONAL_ID = ?', whereArgs: [user.nationalId]);
}
Future close() async {
var dbClient = await db;
dbClient.close();
}
}
validate(BuildContext ctx) async {
if (usernameController.text != "" && passwordController.text != "") {
dbHelper = DBHelper();
User test =
await dbHelper.checkLogin(usernameController.text, passwordController.text);
// just for test what we got here
showFlushBar(ctx, "test", test.toString());
//navigateToUsersList(ctx);
} else {
showFlushBar(ctx, "خطا", "اطلاعات را وارد نمایید");
}
}