我的代码有问题,导致在database_helper.dart中午餐时出错
database_helper.dart是Sqlite中存储数据学习项目的一个文件,我遇到了此问题,我尝试了许多解决方案,例如卸载应用程序,然后重新安装。
我已经编辑了问题,并添加了另一个名为User.dart的代码,希望您找到解决该问题的方法,我真的很困
class DataBaseHelper {
static Database _db ;
final String userT = 'userT' ;
final String columnId = 'id' ;
final String columnUserName = 'username' ;
final String columnPassword = 'password' ;
final String columnAge = 'age' ;
final String columnCity = 'city' ;
Future<Database> get dbase async {
if(_db != null) {
return _db ;
}
_db = await intDB();
return _db ;
}
intDB() async {
Directory docDirectory = await getApplicationDocumentsDirectory() ;
String path = join(docDirectory.path , 'myDB.db') ;
var myOwnDB = await openDatabase(path , version: 1 , onCreate: _onCreate ) ;
return myOwnDB ;
}
void _onCreate(Database db , int newVersion) async {
var sql = 'CREATE TABLE $userT ($columnId INTEGER PRIMARY KEY ,'
' $columnUserName TEXT , $columnPassword TEXT , $columnCity TEXT , $columnAge INTEGER)' ;
await db.execute(sql) ;
}
Future<int> saveInfo(User user) async {
var dbClient = await dbase ;
int result = await dbClient.insert('$userT', user.toMap()) ;
return result ;
}
Future<List> getInfo() async {
var dbClient = await dbase ;
var sql = 'SELECT * FROM $userT' ;
List result = await dbClient.rawQuery(sql) ;
return result.toList() ;
}
Future<int> getCount() async {
var dbClient = await dbase ;
var sql = 'SELECT COUNT(*) FROM $userT' ;
return Sqflite.firstIntValue( await dbClient.rawQuery(sql) ) ;
}
Future<User> getUser(int id) async {
var dbClient = await dbase ;
var sql = 'SELECT * FROM $userT WHERE $columnId = $id' ;
var result = await dbClient.rawQuery(sql) ;
if(result.length == 0) return null ;
return new User.fromMap(result.first) ;
}
Future<int> deleteUser(int id) async {
var dbClient = await dbase ;
return await dbClient.delete(userT , where: '$columnId = ?' , whereArgs: [id] ) ;
}
Future<int> updateUser(User user) async {
var dbClient = await dbase ;
return await dbClient.update(userT , user.toMap() , where: '$columnId = ?' , whereArgs: [user.id] ) ;
}
Future closeDb() async {
var dbClient = await dbase ;
return await dbClient.close() ;
}
}
并且有一个名为User.dart的文件
class User {
String userName ;
String password ;
String userCity ;
int userAge ;
int id ;
User(this.userName , this.password , this.userCity , this.userAge ) ;
User.map(dynamic obj){
this.userName = obj['username'] ;
this.password = obj['password'] ;
this.userAge = obj['age'] ;
this.userCity = obj['city'] ;
this.id = obj['id'] ;
}
String get _uName => userName ;
String get _pass => password ;
String get _uCity => userCity ;
int get _uAge => userAge ;
int get _uId => id ;
Map<String , dynamic> toMap() {
var map = new Map<String , dynamic>() ;
map['username'] = _uName ;
map['password'] = _pass ;
map['city'] = _uCity ;
map['age'] = _uAge ;
if(id != null){
map['id'] = _uId ;
}
return map ;
}
User.fromMap(Map<String , dynamic>map) {
this.userName = map['username'] ;
this.password = map['password'] ;
this.userAge = map['age'] ;
this.userCity = map['city'] ;
this.id = map['id'] ;
}
}
这是错误日志
E/flutter ( 3969): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'
E/flutter ( 3969): #0 new User.map (package:was17savesqllite/model/user.dart:14:25)
E/flutter ( 3969): #1 main (package:was17savesqllite/main.dart:16:22)
E/flutter ( 3969): <asynchronous suspension>
E/flutter ( 3969): #2 _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:216:25)
E/flutter ( 3969): #3 _rootRun (dart:async/zone.dart:1124:13)
E/flutter ( 3969): #4 _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter ( 3969): #5 _runZoned (dart:async/zone.dart:1516:10)
E/flutter ( 3969): #6 runZoned (dart:async/zone.dart:1500:12)
E/flutter ( 3969): #7 _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:208:5)
E/flutter ( 3969): #8 _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:301:19)
E/flutter ( 3969): #9 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:172:12)
E/flutter ( 3969):
我确实在本教程中陷入了困境,请问有人可以帮助我吗?
答案 0 :(得分:0)
动态结果中的数据类型与用户对象尝试投射不匹配
//inside
User.fromMap(Map<String , dynamic>map) {
this.userAge = int.parse(map['age'].toString());
this.id = int.parse(map['id'].toString()) ;
}