我在该页面中有一个称为登录页面的页面,我已经在文本字段和按钮中使用了标签,例如(用户名,登录名),在登录按钮中使用了登录标签,但是我也希望从sqlflite数据库中获取该标签已将数据存储到数据库,但我想检索该数据以保存该标签。
这是此登录表单,窗口小部件标签文本为用户名,但我希望从已经存储到数据库中的SQL数据库(我只是想在标签文本中检索)。 在这里,我尝试了代码。
数据库帮助程序代码
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
import 'package:tudo/src/utils/sql/sql_appconstant_model.dart';
class DbOperation {
static final DbOperation _instance = DbOperation._();
static Database _database;
DbOperation._();
factory DbOperation() {
return _instance;
}
Future<Database> get db async {
if (_database != null) {
return _database;
}
_database = await init();
return _database;
}
Future<Database> init() async {
Directory directory = await getApplicationDocumentsDirectory();
String dbPath = join(directory.path, 'database.db');
var database = openDatabase(dbPath, version: 1, onCreate: _onCreate);
return database;
}
void _onCreate(Database db, int version) {
db.execute('''
CREATE TABLE appConstant(
id INTEGER PRIMARY KEY AUTOINCREMENT,
language TEXT,
screen INTEGER,
value INTEGER,
label INTEGER)
''');
print("Database was created!");
}
Future<int> insertdata(DbAppConstant dbAppConstant) async {
var client = await db;
return client.insert('appConstant', dbAppConstant.toMapForDb(),
conflictAlgorithm: ConflictAlgorithm.replace);
}
Future<List<DbAppConstant>> fetchAll() async {
var client = await db;
var res = await client.query('appConstant');
if (res.isNotEmpty) {
var appConst = res
.map((appConstantMap) => DbAppConstant.fromDb(appConstantMap))
.toList();
return appConst;
}
return [];
}
Future deleteAll() async {
var client = await db;
client.delete('appConstant');
}
Future closeDb() async {
var client = await db;
client.close();
}
}
登录屏幕用户名小部件
Widget _buildUserNameField() {
return EnsureVisibleWhenFocused(
focusNode: _emailFocusNode,
child: TudoEmailWidget(
focusNode: _emailFocusNode,
prefixIcon: Icon(Icons.email),
labelText: "Username",
validator: (val) => Validators.validateEmail(val.trim()),
onSaved: (val) => _username = val.trim(),
// onChanged:(val) => _username = val.trim(),
),
);
}
数据库模型
import 'package:meta/meta.dart';
class DbAppConstant {
@required
final int id;
@required
final String language;
@required
final String screen;
@required
final String value;
@required
final String label;
DbAppConstant({this.id, this.language, this.screen, this.value, this.label});
DbAppConstant.random()
: this.id = null,
this.language = 'en',
this.screen = 'login_screen',
this.value = 'email',
this.label = 'Email';
DbAppConstant.fromDb(Map<String, dynamic> map)
: id = map['id'],
language = map['language'],
screen = map['screen'],
value = map['value'],
label = map['label'];
Map<String, dynamic> toMapForDb() {
var map = Map<String, dynamic>();
map['id'] = id;
map['language'] = language;
map['screen'] = screen;
map['value'] = value;
map['label'] = label;
return map;
}
}