双用户,
我在 iOS 中成功使用带有 sqflite 的数据库运行,
但是我在android中遇到了问题..
DBHelper 代码:
import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';
import 'package:socialapp/model/todo.dart';
import 'dart:async';
import 'dart:io';
import 'package:path/path.dart';
final String tableName = 'person';
class DBHelper{
DBHelper._();
static final DBHelper _db = DBHelper._();
factory DBHelper() => _db;
//해당 변수에 데이터베이스 정보 저장
static Database _database;
Future<Database> get database async{
if(_database == null){
print('데이터베이스 없음 , 초기화 시작');
_database = await initDB();
return _database;}else {
print('이미 데이터베이스가 존재');
return _database;
}
}
initDB() async{
// Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join('/Users/landwhale', 'Person_Pro_file.db');
print('초기화중');
print(path);
return await openDatabase(
path,
version: 1,
onOpen: (db) {},
onCreate: (Database db, int version) async{
await db.execute('''
CREATE TABLE $tableName(
id INTEGER PRIMARY KEY,
name TEXT,
intro TEXT,
age TEXT) ''');
},
);
}
createData(Todo todo)async{
print('데이터베이스 생성중..');
final db = await database;
print('데이터베이스 추가중 ...');
var res = await db.insert(tableName, todo.toJson());
print('데이터베이스 추가완료');
return res;
}
//read
getTodo(int id) async{
final db = await database;
var res = await db.query(tableName, where: 'id = ?', whereArgs: [id]);
return res.isNotEmpty ? Todo.fromJson(res.first) : Null;
}
// getAllTodos() async{
// final db = await database;
// var res = await db.query(tableNam
// }
updateTodo(Todo todo) async{
final db = await database;
var res = await db.update(tableName, todo.toJson(),
where: 'id =?' , whereArgs: [todo.id]);
return res;
}
//Delete
deleteTodo(int id) async{
final db = await database;
db.delete(tableName, where: 'id =?', whereArgs: [id]);
}
deleteAllTodos() async{
final db =await database;
db.rawDelete("Delete * from $tableName");
}
}
待办事项代码:
class Todo {
int id;
String name;
String intro;
String age;
Todo({
this.id,
this.name,
this.intro,
this.age,});
factory Todo.fromJson(Map<String, dynamic> json) => Todo(
id: json["id"],
intro: json["intro"],
name: json["name"],
age: json["age"],
);
Map<String, dynamic> toJson() =>
{
"id" :id,
"name": name,
"age": age,
"intro" : intro,
};
}
错误 :
데이터베이스 생성중..
I/flutter ( 2952): 데이터베이스 없음 , 초기화 시작
I/flutter ( 2952): 초기화중
I/flutter ( 2952): /Users/landwhale/Person_Pro_file.db
E/flutter ( 2952): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: DatabaseException(open_failed /Users/landwhale/Person_Pro_file.db)
E/flutter ( 2952): #0 wrapDatabaseException (package:sqflite/src/exception_impl.dart:11:7)
E/flutter ( 2952): <asynchronous suspension>
E/flutter ( 2952): #1 SqfliteDatabaseFactoryImpl.wrapDatabaseException (package:sqflite/src/factory_impl.dart:29:7)
E/flutter ( 2952): #2 SqfliteDatabaseMixin.safeInvokeMethod (package:sqflite/src/database_mixin.dart:184:15)
E/flutter ( 2952): #3 SqfliteDatabaseMixin.openDatabase (package:sqflite/src/database_mixin.dart:519:15)
E/flutter ( 2952): <asynchronous suspension>
E/flutter ( 2952): #4 SqfliteDatabaseMixin.doOpen (package:sqflite/src/database_mixin.dart:612:28)
E/flutter ( 2952): <asynchronous suspension>
E/flutter ( 2952): #5 SqfliteDatabaseOpenHelper.openDatabase (package:sqflite/src/database.dart:32:22)
E/flutter ( 2952): <asynchronous suspension>
E/flutter ( 2952): #6 SqfliteDatabaseFactoryMixin.openDatabase.<anonymous closure> (package:sqflite/src/factory_mixin.dart:100:43)
E/flutter ( 2952): <asynchronous suspension>
E/flutter ( 2952): #7 ReentrantLock.synchronized.<anonymous closure>.<anonymous closure> (package:synchronized/src/reentrant_lock.dart:33:24)
E/flutter ( 2952): #8 _rootRun (dart:async/zone.dart:1124:13)
E/flutter ( 2952): #9 _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter ( 2952): #10 _runZoned (dart:async/zone.dart:1516:10)
E/flutter ( 2952): #11 runZoned (dart:async/zone.dart:1463:12)
E/flutter ( 2952): #12 ReentrantLock.synchronized.<anonymous closure> (package:synchronized/src/reentrant_lock.dart:32:24)
E/flutter ( 2952): <asynchronous suspension>
E/flutter ( 2952): #13 BasicLock.synchronized (package:synchronized/src/basic_lock.dart:31:26)
E/flutter ( 2952): <asynchronous suspension>
E/flutter ( 2952): #14 ReentrantLock.synchronized (package:synchronized/src/reentrant_lock.dart:28:17)
E/flutter ( 2952): <asynchronous suspension>
E/flutter ( 2952): #15 SqfliteDatabaseFactoryMixin.openDatabase (package:sqflite/src/factory_mixin.dart:66:17)
E/flutter ( 2952): #16 openDatabase (package:sqflite/sqflite.dart:150:26)
E/flutter ( 2952): #17 DBHelper.initDB (package:socialapp/widgets/database_create.dart:36:18)
E/flutter ( 2952): <asynchronous suspension>
E/flutter ( 2952): #18 DBHelper.database (package:socialapp/widgets/database_create.dart:23:25)
E/flutter ( 2952): <asynchronous suspension>
E/flutter ( 2952): #19 DBHelper.createData (package:socialapp/widgets/database_create.dart:54:22)
E/flutter ( 2952): <asynchronous suspension>
E/flutter ( 2952): #20 _writeprofile2State.onPressed (package:socialapp/page/writeprofile2.dart:192:18)
E/flutter ( 2952): #21 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:654:14)
E/flutter ( 2952): #22 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:729:32)
E/flutter ( 2952): #23 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter ( 2952): #24 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:365:11)
E/flutter ( 2952): #25 TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:275:7)
E/flutter ( 2952): #26 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:455:9)
E/flutter ( 2952): #27 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:75:13)
E/flutter ( 2952): #28 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:102:11)
E/flutter ( 2952): #29 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:218:19)
E/flutter ( 2952): #30 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter ( 2952): #31 GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter ( 2952): #32 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter ( 2952): #33 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter ( 2952): #34 _rootRunUnary (dart:async/zone.dart:1136:13)
E/flutter ( 2952): #35 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter ( 2952): #36 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter ( 2952): #37 _invoke1 (dart:ui/hooks.dart:263:10)
E/flutter ( 2952): #38 _dispatchPointerDataPacket (dart:ui/hooks.dart:172:5)
E/flutter ( 2952):
显然,它在 iOS 上运行良好,但在 Android 上却出现了错误。
我以为是版本问题,所以我更新到最新版本,但仍然出现。
如何解决这个问题?
如果有人能帮助我,我将不胜感激。