sqflite + Flutter : map((e) => .... returns [Class 实例]
sqflite + Flutter : map((e) => .... returns [Instance of Class]
我目前正在使用 Sqflite 和 Flutter 开发一个小应用程序。
我的用户模型 Class 如下:
class User {
static const tblUser = 'users';
static const colUserId = 'id';
static const colUserName = 'name';
static const colUserMail = 'mail';
static const colUserPhone = 'phone';
User({this.id, this.name, this.mail, this.phone});
int id;
String name;
String mail;
String phone;
User.fromMap(Map<String, dynamic> map) {
this.id = map[colUserId];
this.name = map[colUserName];
this.mail = map[colUserMail];
this.phone = map[colUserPhone];
}
Map<String, dynamic> toMap() {
var map = <String, dynamic>{
colUserName: name,
colUserMail: mail,
colUserPhone: phone
};
if (id != null) {
map[colUserId] = id;
}
return map;
}
}
在 database_helper class 中,我创建了正确工作的数据库和表:
class DatabaseHelper {
static const _databaseName = 'FinmanDatabase.db';
static const _databaseVersion = 1;
DatabaseHelper._();
static final DatabaseHelper instance = DatabaseHelper._();
Database _database;
Future<Database> get database async {
if (_database != null) return _database;
_database = await _initDatabase();
return _database;
}
_initDatabase() async {
Directory dbDirectory = await getApplicationDocumentsDirectory();
String dbpath = join(dbDirectory.path, _databaseName);
return await openDatabase(dbpath,
version: _databaseVersion, onCreate: _onCreateDB);
}
Future _onCreateDB(Database db, int version) async {
await db.execute('''
CREATE TABLE ${User.tblUser} (
${User.colUserId} INTEGER PRIMARY KEY AUTOINCREMENT,
${User.colUserName} TEXT NOT NULL,
${User.colUserMail} TEXT NOT NULL,
${User.colUserPhone} TEXT NOT NULL
)
''');
}}
但是,当我尝试让用户颤动时,程序 returns 'Instance of User' 而不是内容。获取用户的函数如下:
Future<List<User>> fetchAllUser() async {
Database db = await database;
List<Map> userslist = await db.query(User.tblUser);
int count = userslist.length;
print('Printing $userslist from DBHelper + Length: $count');
return userslist.length == 0 ? [] : userslist.map((e) => User.fromMap(e)).toList();
}
下面是我在 Flutter 中的调用方式:
class _HomeState extends State<Home> {
User user = User();
List<User> _users;
String _dbExists;
DatabaseHelper _dbhelper;
int count = 0;
@override
void initState() {
super.initState();
setState(() {
_dbhelper = DatabaseHelper.instance;
});
_refreshUserList();
}
_refreshUserList() async {
List<User> x = await _dbhelper.fetchAllUser();
setState(() {
_users = x;
print(_users);
count = _users.length;
print(count);
if (count > 0) {
_dbExists = 'Database exists';
} else {
_dbExists = 'Create a new Database';
}
});
}}
而print(count) returns 1(即User中的记录数Table),print(_users) returns [Instance of User]??
感谢任何帮助。
when I try to fetch the Users to flutter, the program returns
'Instance of User' instead of the contents
因为 x 是 List<User>
。
要获取内容,可以使用for-loop
for(var i in x){
print(i.name); // it will print out the name
}
我目前正在使用 Sqflite 和 Flutter 开发一个小应用程序。
我的用户模型 Class 如下:
class User {
static const tblUser = 'users';
static const colUserId = 'id';
static const colUserName = 'name';
static const colUserMail = 'mail';
static const colUserPhone = 'phone';
User({this.id, this.name, this.mail, this.phone});
int id;
String name;
String mail;
String phone;
User.fromMap(Map<String, dynamic> map) {
this.id = map[colUserId];
this.name = map[colUserName];
this.mail = map[colUserMail];
this.phone = map[colUserPhone];
}
Map<String, dynamic> toMap() {
var map = <String, dynamic>{
colUserName: name,
colUserMail: mail,
colUserPhone: phone
};
if (id != null) {
map[colUserId] = id;
}
return map;
}
}
在 database_helper class 中,我创建了正确工作的数据库和表:
class DatabaseHelper {
static const _databaseName = 'FinmanDatabase.db';
static const _databaseVersion = 1;
DatabaseHelper._();
static final DatabaseHelper instance = DatabaseHelper._();
Database _database;
Future<Database> get database async {
if (_database != null) return _database;
_database = await _initDatabase();
return _database;
}
_initDatabase() async {
Directory dbDirectory = await getApplicationDocumentsDirectory();
String dbpath = join(dbDirectory.path, _databaseName);
return await openDatabase(dbpath,
version: _databaseVersion, onCreate: _onCreateDB);
}
Future _onCreateDB(Database db, int version) async {
await db.execute('''
CREATE TABLE ${User.tblUser} (
${User.colUserId} INTEGER PRIMARY KEY AUTOINCREMENT,
${User.colUserName} TEXT NOT NULL,
${User.colUserMail} TEXT NOT NULL,
${User.colUserPhone} TEXT NOT NULL
)
''');
}}
但是,当我尝试让用户颤动时,程序 returns 'Instance of User' 而不是内容。获取用户的函数如下:
Future<List<User>> fetchAllUser() async {
Database db = await database;
List<Map> userslist = await db.query(User.tblUser);
int count = userslist.length;
print('Printing $userslist from DBHelper + Length: $count');
return userslist.length == 0 ? [] : userslist.map((e) => User.fromMap(e)).toList();
}
下面是我在 Flutter 中的调用方式:
class _HomeState extends State<Home> {
User user = User();
List<User> _users;
String _dbExists;
DatabaseHelper _dbhelper;
int count = 0;
@override
void initState() {
super.initState();
setState(() {
_dbhelper = DatabaseHelper.instance;
});
_refreshUserList();
}
_refreshUserList() async {
List<User> x = await _dbhelper.fetchAllUser();
setState(() {
_users = x;
print(_users);
count = _users.length;
print(count);
if (count > 0) {
_dbExists = 'Database exists';
} else {
_dbExists = 'Create a new Database';
}
});
}}
而print(count) returns 1(即User中的记录数Table),print(_users) returns [Instance of User]??
感谢任何帮助。
when I try to fetch the Users to flutter, the program returns 'Instance of User' instead of the contents
因为 x 是 List<User>
。
要获取内容,可以使用for-loop
for(var i in x){
print(i.name); // it will print out the name
}