今回の記事はFlutterでSQLiteを使用できるようにする設定と、データ保存を行う方法についてご紹介します。
FlutteにSQLiteインストール
SQLiteそのものをインストールする必要はありません。いつも通りの「pubget.yaml」で下記を「pub get」するだけでデータベースの作成が行えます。
environment:
sdk: ">=2.7.0 <3.0.0"
dependencies:
sqflite: ^1.3.2+3
flutter:
sdk: flutter
FlutterでSQLiteを使用する方法
次にSQLiteを使用する方法に関してご紹介します。
基本的にSQLiteを使用する際は別クラスとして、記載しておくほうが使用しやすいです。別ファイルに分けて使用するでも良いと思います。基本的に以下の操作が基本になると思うので記載しておきます。
テーブル定義
final int id;
final String text;
Memo({this.id, this.text});
Map<String, dynamic> toMap() {
return {
'id': id,
'text': text
};
}
@override
String toString() {
return 'Memo{id: $id, text: $text}';
}
データベースの作成
static Future<Database> get database async {
final Future<Database> _database = openDatabase(
join(await getDatabasesPath(), 'memo_database.db'),
onCreate: (db, version) {
return db.execute(
"CREATE TABLE memo(id INTEGER PRIMARY KEY AUTOINCREMENT, text TEXT)",
);
},
version: 1,
);
return _database;
}
データのインプット
static Future<void> insertMemo(Memo memo) async {
final Database db = await database;
await db.insert(
'memo',
memo.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
データの確認
static Future<List<Memo>> getMemos() async {
final Database db = await database;
final List<Map<String, dynamic>> maps = await db.query('memo');
return List.generate(maps.length, (i) {
return Memo(
id: maps[i]['id'],
text: maps[i]['text'],
);
});
}
データの更新
static Future<void> updateMemo(Memo memo) async {
final db = await database;
await db.update(
'memo',
memo.toMap(),
where: "id = ?",
whereArgs: [memo.id],
conflictAlgorithm: ConflictAlgorithm.fail,
);
}
データの削除
static Future<void> deleteMemo(int id) async {
final db = await database;
await db.delete(
'memo',
where: "id = ?",
whereArgs: [id],
);
}
FlutterでSQLiteを使用したTodoアプリ例
SQLiteを使用したTODOアプリに関しては下記に記載しておきます。参考までに。
import 'package:flutter/material.dart';
import 'package:sqflite/sqflite.dart';
import 'dart:async';
import 'package:path/path.dart';
class Memo {
final int id;
final String text;
Memo({this.id, this.text});
Map<String, dynamic> toMap() {
return {
'id': id,
'text': text,
};
}
@override
String toString() {
return 'Memo{id: $id, text: $text}';
}
static Future<Database> get database async {
final Future<Database> _database = openDatabase(
join(await getDatabasesPath(), 'memo_database.db'),
onCreate: (db, version) {
return db.execute(
"CREATE TABLE memo(id INTEGER PRIMARY KEY AUTOINCREMENT, text TEXT)",
);
},
version: 1,
);
return _database;
}
static Future<void> insertMemo(Memo memo) async {
final Database db = await database;
await db.insert(
'memo',
memo.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
static Future<List<Memo>> getMemos() async {
final Database db = await database;
final List<Map<String, dynamic>> maps = await db.query('memo');
return List.generate(maps.length, (i) {
return Memo(
id: maps[i]['id'],
text: maps[i]['text'],
);
});
}
static Future<void> updateMemo(Memo memo) async {
final db = await database;
await db.update(
'memo',
memo.toMap(),
where: "id = ?",
whereArgs: [memo.id],
conflictAlgorithm: ConflictAlgorithm.fail,
);
}
static Future<void> deleteMemo(int id) async {
final db = await database;
await db.delete(
'memo',
where: "id = ?",
whereArgs: [id],
);
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo SQL',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MySqlPage(),
);
}
}
class MySqlPage extends StatefulWidget {
@override
_MySqlPageState createState() => _MySqlPageState();
}
class _MySqlPageState extends State<MySqlPage> {
List<Memo> _memoList = [];
final myController = TextEditingController();
final upDateController = TextEditingController();
var _selectedvalue;
Future<void> initializeDemo() async {
_memoList = await Memo.getMemos();
}
@override
void dispose() {
myController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('メモアプリ'),
),
body: Container(
padding: EdgeInsets.all(32),
child: FutureBuilder(
future: initializeDemo(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
// 非同期処理未完了 = 通信中
return Center(
child: CircularProgressIndicator(),
);
}
return ListView.builder(
itemCount: _memoList.length,
itemBuilder: (context, index) {
return Card(
child: ListTile(
leading: Text(
'ID ${_memoList[index].id}',
style: TextStyle(fontWeight: FontWeight.bold),
),
title: Text('${_memoList[index].text}'),
trailing: SizedBox(
width: 76,
height: 25,
child: RaisedButton.icon(
onPressed: () async {
await Memo.deleteMemo(_memoList[index].id);
final List<Memo> memos = await Memo.getMemos();
setState(() {
_memoList = memos;
});
},
icon: Icon(
Icons.delete_forever,
color: Colors.white,
size: 18,
),
label: Text(
'削除',
style: TextStyle(fontSize: 11),
),
color: Colors.red,
textColor: Colors.white,
),
),
),
);
},
);
},
),
),
floatingActionButton: Column(
verticalDirection: VerticalDirection.up,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
showDialog(
context: context,
builder: (_) => AlertDialog(
title: Text("新規メモ作成"),
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text('なんでも入力してね'),
TextField(controller: myController),
RaisedButton(
child: Text('保存'),
onPressed: () async {
Memo _memo = Memo(text: myController.text);
await Memo.insertMemo(_memo);
final List<Memo> memos = await Memo.getMemos();
setState(() {
_memoList = memos;
_selectedvalue = null;
});
myController.clear();
Navigator.pop(context);
},
),
],
),
));
},
),
SizedBox(height: 20),
FloatingActionButton(
child: Icon(Icons.update),
backgroundColor: Colors.amberAccent,
onPressed: () async {
await showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: StatefulBuilder(
builder:
(BuildContext context, StateSetter setState) {
return Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text('IDを選択して更新してね'),
Row(
children: <Widget>[
Flexible(
flex: 1,
child: DropdownButton(
hint: Text("ID"),
value: _selectedvalue,
onChanged: (newValue) {
setState(() {
_selectedvalue = newValue;
print(newValue);
});
},
items: _memoList.map((entry) {
return DropdownMenuItem(
value: entry.id,
child: Text(entry.id.toString()));
}).toList(),
),
),
Flexible(
flex: 3,
child: TextField(
controller: upDateController),
),
],
),
RaisedButton(
child: Text('更新'),
onPressed: () async {
Memo updateMemo = Memo(
id: _selectedvalue,
text: upDateController.text);
await Memo.updateMemo(updateMemo);
final List<Memo> memos =
await Memo.getMemos();
super.setState(() {
_memoList = memos;
});
upDateController.clear();
Navigator.pop(context);
},
),
],
);
},
),
);
});
}),
],
),
);
}
}
今回の記事は以上です。他にもFlutterの記事を多数書いておりますのできになる方はご参考ください。
コメント