简体   繁体   中英

How Can I Solve This Problem in my flutter code sqlite

I have a problem in my code that occurs error in lunching in database_helper.dart

database_helper.dart is one file in Sqlite storing data learning project and i faced this issue , i tried many solutions like uninstall app and install it again .

I have edited my question and added the other code called User.dart, I hope you find a solution to this problem, I'm really stuck

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() ;
  }


}

and there is the file that called 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'] ;

   }
}

and this is the error log

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):

im really stuck here in tutorial , can any one help me plz ?

Looks like data types in your dynamic result does not match User object try casting

//inside 
User.fromMap(Map<String , dynamic>map) {
this.userAge = int.parse(map['age'].toString());
this.id = int.parse(map['id'].toString()) ;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM