颤振类型 'int' 不是 'String' 类型的子类型,尽管没有使用整数



我修复了它一次,但它回来了,我不明白为什么我确信我的代码是相同的之前,它的工作。主要要注意的是HomeForm。LoginForm飞镖。dart和DbHelper。但我看不出有什么问题。请帮我指出这个问题,或者这是什么样的错误,因为在这种情况下,我真的没有玩任何变量。刚刚发生了,我试着卸载手机上的应用程序,重新加载它仍然没有运气。

DbHelper.dart:

import 'package:conferenceApp/Model/UserModel.dart';
import 'package:conferenceApp/Model/specializeModel.dart';
import 'package:conferenceApp/Model/loginModel.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
import 'dart:io' as io;
class DbHelper {
static Database conference;
static const String DB_Name = 'conference.db';
static const String Table_conferenceInfo = 'conferenceinfo';
static const String Table_specializeArea = 'specializearea';
static const String Table_login = 'login';
static const String C_UserID = 'id';
static const String C_Name = 'name';
static const String C_UName = 'username';
static const String C_Email = 'email';
static const String C_Phone = 'phone';
static const String C_Role = 'role';
static const String C_Area = 'area';
static const String C_Password = 'password';
static const int Version = 1;
Future<Database> get db async {
if (conference != null) {
return conference;
}
conference = await initDb();
return conference;
}
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_conferenceInfo ("
" $C_UserID INTEGER, "
" $C_Name TEXT, "
" $C_UName TEXT, "
" $C_Email TEXT,"
" $C_Password TEXT, "
" $C_Phone INTEGER, "
" $C_Role TEXT, "
" PRIMARY KEY ($C_UserID)"
")");
await db.execute("CREATE TABLE $Table_specializeArea ("
" $C_UserID INTEGER, "
" $C_Area TEXT )");
await db.execute("CREATE TABLE $Table_login ("
" $C_UserID INTEGER, "
" $C_UName TEXT, "
" $C_Password TEXT )");
}
Future<void> saveData(UserModel user) async {
var dbClient = await db;
var res = await dbClient.insert(Table_conferenceInfo, user.toMap());
return res;
}
Future<int> saveData1(specializeModel user) async {
var dbClient = await db;
var res1 = await dbClient.insert(Table_specializeArea, user.toMap());
return res1;
}
Future<int> saveData2(loginModel user) async {
var dbClient = await db;
var res2 = await dbClient.insert(Table_login, user.toMap());
return res2;
}
Future<UserModel> getLoginUser(String userId, String password) async {
var dbClient = await db;
var res =
await dbClient.rawQuery("SELECT * FROM $Table_conferenceInfo WHERE "
"$C_UName = '$userId' AND "
"$C_Password = '$password'");
if (res.length > 0) {
return UserModel.fromMap(res.first);
}
return null;
}
Future<int> updateUser(UserModel user) async {
var dbClient = await db;
var res = await dbClient.update(Table_conferenceInfo, user.toMap(),
where: '$C_UserID = ?', whereArgs: [user.user_id]);
return res;
}
Future<int> deleteUser(String user_id) async {
var dbClient = await db;
var res = await dbClient.delete(Table_conferenceInfo,
where: '$C_UserID = ?', whereArgs: [user_id]);
return res;
}
}

LoginForm.dart:

import 'package:conferenceApp/Model/loginModel.dart';
import 'package:flutter/material.dart';
import 'package:conferenceApp/Comm/comHelper.dart';
import 'package:conferenceApp/Comm/genLoginSignupHeader.dart';
import 'package:conferenceApp/Comm/genTextFormField.dart';
import 'package:conferenceApp/DatabaseHandler/DbHelper.dart';
import 'package:conferenceApp/Model/UserModel.dart';
import 'package:conferenceApp/Screens/SignupForm.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'HomeForm.dart';
class LoginForm extends StatefulWidget {
@override
_LoginFormState createState() => _LoginFormState();
}
class _LoginFormState extends State<LoginForm> {
Future<SharedPreferences> _pref = SharedPreferences.getInstance();
final _formKey = new GlobalKey<FormState>();
final _conUserName = TextEditingController();
final _conPassword = TextEditingController();
var dbHelper;
@override
void initState() {
super.initState();
dbHelper = DbHelper();
}
login() async {
String uname = _conUserName.text;
String passwd = _conPassword.text;
if (uname.isEmpty) {
alertDialog(context, "Please Enter Username");
} else if (passwd.isEmpty) {
alertDialog(context, "Please Enter Password");
} else {
await dbHelper.getLoginUser(uname, passwd).then((userData) {
if (userData != null) {
setSP(userData).whenComplete(() {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (_) => HomeForm()),
(Route<dynamic> route) => true);
});
} else {
alertDialog(context, "Error: User Not Found");
}
}).catchError((error) {
print(error);
alertDialog(context, "Error: Login Fail");
});
}
}
Future setSP(UserModel user) async {
final SharedPreferences sp = await _pref;
sp.setString("id", user.user_id);
sp.setString("name", user.Name);
sp.setString("username", user.user_name);
sp.setString("email", user.email);
sp.setString("password", user.password);
sp.setString("phone", user.phone);
sp.setString("role", user.role);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Login with Signup'),
),
body: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Container(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
genLoginSignupHeader('Login'),
getTextFormField(
controller: _conUserName,
icon: Icons.person,
hintName: 'Username'),
SizedBox(height: 10.0),
getTextFormField(
controller: _conPassword,
icon: Icons.lock,
hintName: 'Password',
isObscureText: true,
),
Container(
margin: EdgeInsets.all(30.0),
width: double.infinity,
child: FlatButton(
child: Text(
'Login',
style: TextStyle(color: Colors.white),
),
onPressed: login,
),
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(30.0),
),
),
Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Does not have account? '),
FlatButton(
textColor: Colors.blue,
child: Text('Signup'),
onPressed: () {
Navigator.push(context,
MaterialPageRoute(builder: (_) => SignupForm()));
},
)
],
),
),
],
),
),
),
),
);
}
}

UserModel.dart:

class UserModel {
String user_id;
String Name;
String user_name;
String email;
String password;
String phone;
String role;
UserModel(this.user_id, this.Name, this.user_name, this.email, this.password,
this.phone, this.role);
Map<String, dynamic> toMap() {
var map = <String, dynamic>{
'id': user_id,
'name': Name,
'username': user_name,
'email': email,
'password': password,
'phone': phone,
'role': role
};
return map;
}
UserModel.fromMap(Map<String, dynamic> map) {
user_id = map['id'];
Name = map['name'];
user_name = map['username'];
email = map['email'];
password = map['password'];
phone = map['phone'];
role = map['role'];
}
}

SignupForm.dart:

import 'package:conferenceApp/Comm/genTextFormFieldNum.dart';
import 'package:flutter/material.dart';
import 'package:conferenceApp/Comm/comHelper.dart';
import 'package:conferenceApp/Comm/genLoginSignupHeader.dart';
import 'package:conferenceApp/Comm/genTextFormField.dart';
import 'package:conferenceApp/DatabaseHandler/DbHelper.dart';
import 'package:conferenceApp/Model/UserModel.dart';
import 'package:conferenceApp/Model/loginModel.dart';
import 'package:conferenceApp/Screens/LoginForm.dart';
class SignupForm extends StatefulWidget {
@override
_SignupFormState createState() => _SignupFormState();
}
class _SignupFormState extends State<SignupForm> {
final _formKey = new GlobalKey<FormState>();
final _conUserId = TextEditingController();
final _conUserName = TextEditingController();
final _conName = TextEditingController();
final _conEmail = TextEditingController();
final _conPassword = TextEditingController();
final _conCPassword = TextEditingController();
final _conCPhone = TextEditingController();
final _conRole = TextEditingController();
var dbHelper;
@override
void initState() {
super.initState();
dbHelper = DbHelper();
}
signUp() async {
String uid = _conUserId.text;
String uname = _conUserName.text;
String Name = _conName.text;
String email = _conEmail.text;
String passwd = _conPassword.text;
String phone = _conCPhone.text;
String role = _conRole.text;
String cpasswd = _conCPassword.text;
if (_formKey.currentState.validate()) {
if (passwd != cpasswd) {
alertDialog(context, 'Password Mismatch');
} else {
_formKey.currentState.save();
UserModel uModel =
UserModel(uid, Name, uname, email, passwd, phone, role);
loginModel uModel2 = loginModel(uid, uname, passwd);
await dbHelper.saveData2(uModel2).then((userData2) {});
await dbHelper.saveData(uModel).then((userData) {
alertDialog(context, "Successfully Saved");
Navigator.push(
context, MaterialPageRoute(builder: (_) => LoginForm()));
}).catchError((error) {
print(error);
alertDialog(context, "Error: Data Save Fail");
});
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Login with Signup'),
),
body: Form(
key: _formKey,
child: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Container(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
genLoginSignupHeader('Signup'),
getTextFormFieldNum(
controller: _conUserId,
icon: Icons.person,
hintName: 'User ID'),
SizedBox(height: 10.0),
getTextFormField(
controller: _conUserName,
icon: Icons.person_outline,
inputType: TextInputType.name,
hintName: 'Username'),
SizedBox(height: 10.0),
getTextFormField(
controller: _conName,
icon: Icons.person_outline,
inputType: TextInputType.name,
hintName: 'Name'),
SizedBox(height: 10.0),
getTextFormField(
controller: _conEmail,
icon: Icons.email,
inputType: TextInputType.emailAddress,
hintName: 'Email'),
SizedBox(height: 10.0),
getTextFormField(
controller: _conPassword,
icon: Icons.lock,
hintName: 'Password',
isObscureText: true,
),
SizedBox(height: 10.0),
getTextFormField(
controller: _conCPassword,
icon: Icons.lock,
hintName: 'Confirm Password',
isObscureText: true,
),
SizedBox(height: 10.0),
getTextFormFieldNum(
controller: _conCPhone,
icon: Icons.phone,
hintName: 'Phone Number',
),
SizedBox(height: 10.0),
getTextFormField(
controller: _conRole,
icon: Icons.remove_red_eye,
hintName: 'Role (Participant, Presenter, Reviewer, Judges',
),
// AREA SELECTION
Container(
margin: EdgeInsets.all(30.0),
width: double.infinity,
child: FlatButton(
child: Text(
'Signup',
style: TextStyle(color: Colors.white),
),
onPressed: signUp,
),
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(30.0),
),
),
Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Does you have account? '),
FlatButton(
textColor: Colors.blue,
child: Text('Sign In'),
onPressed: () {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (_) => LoginForm()),
(Route<dynamic> route) => false);
},
)
],
),
),
],
),
),
),
),
),
);
}
}

loginModel.dart:

class loginModel {
String user_id;
String user_name;
String password;
loginModel(this.user_id, this.user_name, this.password);
Map<String, dynamic> toMap() {
var map = <String, dynamic>{
'id': user_id,
'username': user_name,
'password': password
};
return map;
}
loginModel.fromMap(Map<String, dynamic> map) {
user_id = map['id'];
user_name = map['username'];
password = map['password'];
}
}

HomeForm.dart:

import 'package:flutter/material.dart';
import 'package:conferenceApp/Comm/comHelper.dart';
import 'package:conferenceApp/Comm/genTextFormField.dart';
import 'package:conferenceApp/DatabaseHandler/DbHelper.dart';
import 'package:conferenceApp/Model/UserModel.dart';
import 'package:conferenceApp/Screens/LoginForm.dart';
import 'package:shared_preferences/shared_preferences.dart';
class HomeForm extends StatefulWidget {
@override
_HomeFormState createState() => _HomeFormState();
}
class _HomeFormState extends State<HomeForm> {
final _formKey = new GlobalKey<FormState>();
Future<SharedPreferences> _pref = SharedPreferences.getInstance();
DbHelper dbHelper;
final _conUserId = TextEditingController();
final _conName = TextEditingController();
final _conDelUserId = TextEditingController();
final _conUserName = TextEditingController();
final _conEmail = TextEditingController();
final _conPassword = TextEditingController();
final _conCPhone = TextEditingController();
final _conRole = TextEditingController();
@override
void initState() {
super.initState();
getUserData();
dbHelper = DbHelper();
}
Future<void> getUserData() async {
final SharedPreferences sp = await _pref;
setState(() {
_conUserId.text = sp.getString("id");
_conDelUserId.text = sp.getString("id");
_conName.text = sp.getString("name");
_conUserName.text = sp.getString("username");
_conEmail.text = sp.getString("email");
_conPassword.text = sp.getString("password");
_conCPhone.text = sp.getString("phone");
_conRole.text = sp.getString("role");
});
}
update() async {
String uid = _conUserId.text;
String Name = _conName.text;
String uname = _conUserName.text;
String email = _conEmail.text;
String passwd = _conPassword.text;
String phone = _conCPhone.text;
String role = _conRole.text;
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
UserModel user = UserModel(uid, Name, uname, email, passwd, phone, role);
await dbHelper.updateUser(user).then((value) {
if (value == 1) {
alertDialog(context, "Successfully Updated");
updateSP(user, true).whenComplete(() {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (_) => LoginForm()),
(Route<dynamic> route) => false);
});
} else {
alertDialog(context, "Error Update");
}
}).catchError((error) {
print(error);
alertDialog(context, "Error");
});
}
}
delete() async {
String delUserID = _conDelUserId.text;
await dbHelper.deleteUser(delUserID).then((value) {
if (value == 1) {
alertDialog(context, "Successfully Deleted");
updateSP(null, false).whenComplete(() {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (_) => LoginForm()),
(Route<dynamic> route) => false);
});
}
});
}
Future updateSP(UserModel user, bool add) async {
final SharedPreferences sp = await _pref;
if (add) {
sp.setString("username", user.user_name);
sp.setString("Name", user.Name);
sp.setString("email", user.email);
sp.setString("password", user.password);
sp.setString("phone", user.phone);
sp.setString("role", user.role);
} else {
sp.remove('id');
sp.remove('name');
sp.remove('username');
sp.remove('email');
sp.remove('password');
sp.remove('phone');
sp.remove('role');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home'),
),
body: Form(
key: _formKey,
child: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Container(
margin: EdgeInsets.only(top: 20.0),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
//Update
getTextFormField(
controller: _conUserId,
isEnable: false,
icon: Icons.person,
hintName: 'User ID'),
SizedBox(height: 10.0),
getTextFormField(
controller: _conName,
icon: Icons.person,
hintName: 'Name'),
SizedBox(height: 10.0),
getTextFormField(
controller: _conUserName,
icon: Icons.person_outline,
inputType: TextInputType.name,
hintName: 'Username'),
SizedBox(height: 10.0),
getTextFormField(
controller: _conEmail,
icon: Icons.email,
inputType: TextInputType.emailAddress,
hintName: 'Email'),
SizedBox(height: 10.0),
getTextFormField(
controller: _conPassword,
icon: Icons.lock,
hintName: 'Password',
isObscureText: true,
),
SizedBox(height: 10.0),
getTextFormField(
controller: _conCPhone,
icon: Icons.phone,
hintName: 'Phone',
),
SizedBox(height: 10.0),
getTextFormField(
controller: _conRole,
icon: Icons.remove_red_eye,
hintName: 'Role',
),
SizedBox(height: 10.0),
Container(
margin: EdgeInsets.all(30.0),
width: double.infinity,
child: FlatButton(
child: Text(
'Update',
style: TextStyle(color: Colors.white),
),
onPressed: update,
),
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(30.0),
),
),
//Delete
getTextFormField(
controller: _conDelUserId,
isEnable: false,
icon: Icons.person,
hintName: 'User ID'),
SizedBox(height: 10.0),
SizedBox(height: 10.0),
Container(
margin: EdgeInsets.all(30.0),
width: double.infinity,
child: FlatButton(
child: Text(
'Delete',
style: TextStyle(color: Colors.white),
),
onPressed: delete,
),
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(30.0),
),
),
],
),
),
),
),
),
);
}
}

创建Table_conferenceinfo可能存在问题.因为这里使用的两列都是整数类型

" $C_UserID INTEGER, "
" $C_Phone INTEGER, "

和在你的UserModel中,你使用String数据类型的一切。也许这是个问题。

最新更新