我在做一个应用程序。我将数据库连接到应用程序,在注册时,我将用户名列表与输入数据进行比较,如果它们匹配,我应该得到一个错误。问题是,而不是列表与用户名我得到“示例'Future<List<ConnectifyModel>>>
,如何解决这个问题,可能我没有写正确的查询。
db_handler.dart:
import 'package:connectify_app/model.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';
import 'dart:io' as io;
import 'package:path/path.dart';
class DB {
static Database? _db;
Future<Database?> get db async {
if (_db != null) {
return _db;
}
_db = await initDatabase();
return null;
}
initDatabase() async {
io.Directory documentDirectory = await getApplicationDocumentsDirectory();
String path = join(documentDirectory.path, "Connectify.db");
for(int i = 1; i < 100; i++) {
print(documentDirectory.path);
}
var db = await openDatabase(path, version: 1, onCreate: _createDatabase);
return db;
}
_createDatabase(Database db, int version) async {
await db.execute(
"CREATE TABLE users(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, username TEXT NOT NULL, email TEXT NOT NULL,password TEXT NOT NULL,city TEXT NOT NULL, bithday TEXT NOT NULL, hobbies TEXT)",
);
}
Future<List<ConnectifyModel>> getAllUsernames() async {
await db;
final List<Map<String, Object?>> QueryResult = await _db!.rawQuery(
"SELECT username FROM users");
return QueryResult.map((e) => ConnectifyModel.fromMap(e)).toList();
}
registration.dart:
import 'package:connectify_app/db_handler.dart';
import 'package:connectify_app/login.dart';
import 'package:connectify_app/model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_field_validator/flutter_field_validator.dart';
class Registration extends StatefulWidget {
const Registration({super.key});
@override
State<Registration> createState() => _RegistrationState();
}
class _RegistrationState extends State<Registration> {
DB? db;
late Future<List<ConnectifyModel>> usernames;
List usernameList = [];
String? _name;
String? _username;
String? _email;
String _password = "";
String? _city;
String? _bithday;
String errorPassword = "";
String errorUsername = "";
bool isButtonActive = true;
late TextEditingController controller;
late TextEditingController _namecontroller;
late TextEditingController _usernamecontroller;
late TextEditingController _emailcontroller;
late TextEditingController _passwordcontroller;
late TextEditingController _citycontroller;
late TextEditingController _bithdaycontroller;
@override
void initState(){
super.initState();
controller = TextEditingController();
_namecontroller = TextEditingController();
_usernamecontroller = TextEditingController();
_emailcontroller = TextEditingController();
_passwordcontroller = TextEditingController();
_citycontroller = TextEditingController();
_bithdaycontroller = TextEditingController();
controller.addListener(() {
final isButtonActive = controller.text.isNotEmpty;
setState(() => this.isButtonActive = isButtonActive);
});
db = DB();
loadUser();
}
loadUser() async{
usernames = db!.getAllUsernames();
}
@override
void dispose(){
controller.dispose();
_namecontroller.dispose();
_usernamecontroller.dispose();
_emailcontroller.dispose();
_passwordcontroller.dispose();
_citycontroller.dispose();
_bithdaycontroller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
height: MediaQuery.of(context).size.height,
child: Column(
children: [
SizedBox(
height: MediaQuery.of(context).size.height*0.07,
),
SizedBox(
height: MediaQuery.of(context).size.height*0.05,
child: Text(
"Регистрация",
style: TextStyle(
color: Color.fromRGBO(255, 166, 10, 1),
fontSize: MediaQuery.of(context).size.width*0.1,
),
),
),
SizedBox(
height: MediaQuery.of(context).size.height*0.05,
width: MediaQuery.of(context).size.width,
),
Column(
children: [
SizedBox(
height: MediaQuery.of(context).size.height*0.1,
child: Container(
width: MediaQuery.of(context).size.width*0.85,
child: TextFormField(
onChanged: (String value){
_name = value;
},
decoration: InputDecoration(
filled: true,
fillColor: Color.fromRGBO(255, 166, 10, 0.6),
border: OutlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.all(
Radius.circular(20),
),
),
hintText: "Имя",
hintStyle: TextStyle(color: Colors.white)
),
),
),
),
],
),
Column(
children: [
SizedBox(
height: MediaQuery.of(context).size.height*0.1,
child: Container(
width: MediaQuery.of(context).size.width*0.85,
child: TextFormField(
onChanged: (String value){
setState(() {
_username = value;
print(_username);
print(usernameList);
print(usernames);
if (usernameList.contains(_username)){
errorUsername = "Такой никнем уже существует";
isButtonActive = false;
}
else{
errorUsername = "";
}
});
},
decoration: InputDecoration(
filled: true,
fillColor: Color.fromRGBO(255, 166, 10, 0.6),
border: OutlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.all(
Radius.circular(20)
),
),
hintText: "Ник",
hintStyle: TextStyle(color: Colors.white)
),
),
),
),
],
),
Column(
children: [
SizedBox(
height: MediaQuery.of(context).size.height*0.1,
child: Container(
width: MediaQuery.of(context).size.width*0.85,
child: TextFormField(
onChanged: (String value){
_email = value;
},
// validator: MultiV,
decoration: InputDecoration(
filled: true,
fillColor: Color.fromRGBO(255, 166, 10, 0.6),
border: OutlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.all(
Radius.circular(20)
),
),
hintText: "Email",
hintStyle: TextStyle(color: Colors.white),
helperText: "[email protected]",
helperStyle: TextStyle()
),
),
),
),
],
),
Column(
children: [
SizedBox(
height: MediaQuery.of(context).size.height*0.1,
child: Container(
width: MediaQuery.of(context).size.width*0.85,
child: TextFormField(
obscureText: true,
onChanged: (String value){
setState(() {
_password = value;
if (_password.length < 6){
errorPassword = "Пароль должен содержать как минимум 6 символов";
isButtonActive = false;
}
else{
errorPassword = "";
}
});
},
decoration: InputDecoration(
filled: true,
fillColor: Color.fromRGBO(255, 166, 10, 0.6),
border: OutlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.all(
Radius.circular(20)
),
),
hintText: "Пароль",
hintStyle: TextStyle(color: Colors.white),
errorText: errorPassword.isEmpty?null:errorPassword,
// errorStyle: TextStyle(color: Colors.red)
),
),
),
),
],
),
Column(
children: [
SizedBox(
height: MediaQuery.of(context).size.height*0.1,
child: Container(
width: MediaQuery.of(context).size.width*0.85,
child: TextFormField(
onChanged: (String value){
_city = value;
},
decoration: InputDecoration(
filled: true,
fillColor: Color.fromRGBO(255, 166, 10, 0.6),
border: OutlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.all(
Radius.circular(20)
),
),
hintText: "Город",
hintStyle: TextStyle(color: Colors.white)
),
),
),
),
],
),
Column(
children: [
SizedBox(
height: MediaQuery.of(context).size.height*0.1,
child: Container(
width: MediaQuery.of(context).size.width*0.85,
child: TextFormField(
onChanged: (String value){
_bithday = value;
},
controller: controller,
decoration: InputDecoration(
filled: true,
fillColor: Color.fromRGBO(255, 166, 10, 0.6),
border: OutlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.all(
Radius.circular(20)
),
),
hintText: "Дата рождения",
hintStyle: TextStyle(color: Colors.white),
helperText: "ДД.ММ.ГГГГ"
),
),
),
),
],
),
SizedBox(
height: MediaQuery.of(context).size.height*0.07,
),
TextButton(
style: TextButton.styleFrom(
foregroundColor: Color.fromRGBO(255, 166, 10, 1),
),
onPressed: (){
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
Login()));
},
child: Text(
"У вас уже есть аккаунт?"
),
),
SizedBox(
height: MediaQuery.of(context).size.height*0.05,
width: MediaQuery.of(context).size.width*0.7,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
onSurface: Color.fromRGBO(171, 0, 144, 1) ,
backgroundColor: Color.fromRGBO(171, 0, 144, 1),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20)
),
),
onPressed: (){},
child: Center(
child: Text(
"Зарегистрироваться",
style: TextStyle(
color: Colors.white,
fontSize: MediaQuery.of(context).size.width*0.05
),
),
),),
),
],
),
),
);
}
}
“我试着重做查询,但没有用。
1条答案
按热度按时间hc2pp10m1#
你能试着在这里加上“等待”吗?
像这样定义列表;