Flutter : sqliteの利用

Flutter + Firebase でアプリ作成のためのパーツあつめ。

1.ここまでに確認したこと

  1. Flutter+Firebase環境設定
  2. 画面遷移
  3. Dialog
  4. Httpクライアント
  5. AndroidX対応
  6. 画像の選択
  7. 画像の切り抜き
  8. HTTP POSTと結果JSONの処理

2.今回の確認

sqliteの利用

sqflite パッケージの利用

sqfliteのページのチュートリアルを見ながらコードを記述し、動作確認

  1. import 'package:path/path.dart';
  2. import 'package:sqflite/sqflite.dart';
  3. class DbTest {
  4. void firstDbTest() async {
  5. // OSなど環境別のDBディレクトリパスを取得する
  6. var dbPath = await getDatabasesPath();
  7. // DBファイルパス
  8. dbPath = join(dbPath, 'demo.db');
  9. print("DB Path $dbPath");
  10.  
  11. var helper = MemoProvider();
  12. Database db = await helper.open(dbPath);
  13.  
  14. print("DB: ${db.toString()}");
  15.  
  16. var list = await helper.getAll();
  17. if (list.length < 10) {
  18. var newMemo = Memo();
  19. newMemo.memo = "Memo ${DateTime.now()}";
  20. helper.insert(newMemo);
  21. }
  22.  
  23. for(Memo memo in list) {
  24. print("MEMO : ${memo.toMap().toString()}");
  25. }
  26. }
  27. }
  28.  
  29. class MemoMeta {
  30. static String tableName = "Memo";
  31. static String columnId = "_id";
  32. static String columnMemo = "memo";
  33.  
  34. static List<String> columns() {
  35. return [columnId, columnMemo];
  36. }
  37. }
  38.  
  39. class Memo {
  40. int id;
  41. String memo;
  42.  
  43. Map<String, dynamic> toMap() {
  44. var map = <String, dynamic> {
  45. MemoMeta.columnMemo: memo
  46. };
  47. if (id != null) {
  48. map[MemoMeta.columnId] = id;
  49. }
  50. return map;
  51. }
  52.  
  53. Memo();
  54.  
  55. Memo.fromMap(Map<String, dynamic> map) {
  56. id = map[MemoMeta.columnId];
  57. memo = map[MemoMeta.columnMemo];
  58. }
  59. }
  60.  
  61. class MemoProvider {
  62. Database db;
  63.  
  64. Future<Database> open(String path) async {
  65. var ddl = """
  66. create table ${MemoMeta.tableName} (
  67. ${MemoMeta.columnId} integer primary key autoincrement,
  68. ${MemoMeta.columnMemo} text not null )
  69. """;
  70. print("DDL: $ddl");
  71.  
  72. db = await openDatabase(path, version: 1,
  73. onCreate: (Database db, int version) async {
  74. await db.execute(ddl);
  75. }
  76. );
  77. return db;
  78. }
  79.  
  80. Future<Memo> insert(Memo memo) async {
  81. memo.id = await db.insert(MemoMeta.tableName, memo.toMap());
  82. return memo;
  83. }
  84.  
  85. Future<List<Memo>> getAll() async {
  86. List<Map> maps = await db.query(
  87. MemoMeta.tableName,
  88. columns : MemoMeta.columns());
  89. var list = new List<Memo>();
  90. for(Map map in maps) {
  91. list.add(Memo.fromMap(map));
  92. }
  93. return list;
  94. }
  95.  
  96. Future<Memo> findById(int id) async {
  97. List<Map> maps = await db.query(MemoMeta.tableName,
  98. columns: MemoMeta.columns(),
  99. where: '${MemoMeta.columnId} = ?',
  100. whereArgs: [id]);
  101. if (maps.length > 0) {
  102. return Memo.fromMap(maps.first);
  103. }
  104. return null;
  105. }
  106.  
  107. Future<int> delete(int id) async {
  108. return await db.delete(
  109. MemoMeta.tableName, where: '${MemoMeta.columnId} = ?', whereArgs: [id]);
  110. }
  111.  
  112. Future<int> update(Memo memo) async {
  113. return await db.update(MemoMeta.tableName, memo.toMap(),
  114. where: '${MemoMeta.columnId} = ?', whereArgs: [memo.id]);
  115. }
  116.  
  117. Future close() async => db.close();
  118.  
  119. }

ログに出力された。

  1. I/flutter ( 3116): DB Path /data/user/0/info.typea.favo_phrase_trial/databases/demo.db
  2. I/flutter ( 3116): DDL: create table Memo (
  3. I/flutter ( 3116): _id integer primary key autoincrement,
  4. I/flutter ( 3116): memo text not null )
  5. I/flutter ( 3116): DB: 2 /data/user/0/info.typea.favo_phrase_trial/databases/demo.db
  6. V/AudioManager( 3116): playSoundEffect effectType: 0
  7. V/AudioManager( 3116): querySoundEffectsEnabled...
  8. I/flutter ( 3116): MEMO : {memo: Memo 2019-12-09 21:18:12.816597, _id: 1}
  9. I/flutter ( 3116): MEMO : {memo: Memo 2019-12-09 21:18:17.761803, _id: 2}

Follow me!

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です