我试图在注册页面检查之前保存在表中的用户名和密码,如果用户名和密码已经存在表中,则处理用户导航并导航到另一个屏幕用户。
但当我试图用这种方法访问该表并检查用户名和密码时。
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);
}
我得到了这个错误。Another exception was thrown: type 'Future<dynamic>' is not a subtype of type 'String'
may please guide me on how I must change this method to get username and password and then according to the result handle user navigation? thanks for your help.
编辑。
login_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),
),
),
)),
],
),
)
],
),
),
),
);
}
}
db_helper.dart。
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();
}
}
你的 checkLogin
是一个未来,所以你需要这样在你的生活中等待它。validate
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, "خطا", "اطلاعات را وارد نمایید");
}
}