TuG*_*llo 5 sqlite persistence converters flutter
使用flutter我有这个模型
class Task {
String color;
String env;
String id;
String name;
String description;
bool isBuyIt;
bool isOnBacklog;
}
Run Code Online (Sandbox Code Playgroud)
我使用SwitchListTile是为了更改isBuyItand的布尔值isOnBacklog
SwitchListTile(
activeColor: HexColor(task.color),
title: Text("Do you have it?"),
value: task.isBuyIt,
onChanged: (bool value) {
setState(() {
task.isBuyIt = value;
});
},
secondary: IconButton(
icon: Icon(Icons.shopping_cart),
onPressed: null,
color: HexColor(task.color),
),
)
Run Code Online (Sandbox Code Playgroud)
我正在使用sqflite: ^1.3.0,正如你所知,它不支持 bool value。我这样制作表格:
Future _onCreate(Database db, int version) async {
await db.execute('''
CREATE TABLE $table (
$columnId TEXT PRIMARY KEY,
$columnName TEXT NOT NULL,
$columnColor TEXT NOT NULL,
$columnEnv TEXT NOT NULL,
$columnDescription TEXT NOT NULL,
$columnisBuyIt INTEGER NOT NULL,
$columnisOnBacklog INTEGER NOT NULL
)
''');
}
Run Code Online (Sandbox Code Playgroud)
但我不知道如何将Boolean价值转化为Integer价值。我不想将模型字段更改为,Integer因为它SwitchListTile不适用于INT值
我想Check Constraint会起作用的。
从SQLite 3.23.0 开始,文字 true/false被识别并可以使用。
将 TRUE 和 FALSE 识别为常量。(为了兼容性,如果存在名为“true”或“false”的列,则标识符引用这些列而不是布尔常量。)
CREATE TABLE a (id INT, i BOOLEAN);
INSERT INTO a(id,i) VALUES(10,true);
INSERT INTO a(id,i) VALUES(20, false);
SELECT * FROM a;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7080 次 |
| 最近记录: |