diff --git a/example/lib/database.g.dart b/example/lib/database.g.dart index 5df22eee..5e57e919 100644 --- a/example/lib/database.g.dart +++ b/example/lib/database.g.dart @@ -82,7 +82,7 @@ class _$FlutterDatabase extends FlutterDatabase { }, onCreate: (database, version) async { await database.execute( - 'CREATE TABLE IF NOT EXISTS `Task` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `message` TEXT NOT NULL, `isRead` INTEGER, `timestamp` INTEGER NOT NULL, `type` INTEGER NOT NULL)'); + 'CREATE TABLE IF NOT EXISTS `Task` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `message` TEXT NOT NULL, `isRead` INTEGER NOT NULL, `timestamp` INTEGER NOT NULL, `type` INTEGER NOT NULL)'); await callback?.onCreate?.call(database, version); }, @@ -105,7 +105,7 @@ class _$TaskDao extends TaskDao { (Task item) => { 'id': item.id, 'message': item.message, - 'isRead': item.isRead == null ? null : (item.isRead! ? 1 : 0), + 'isRead': item.isRead ? 1 : 0, 'timestamp': _dateTimeConverter.encode(item.timestamp), 'type': item.type.index }, @@ -117,7 +117,7 @@ class _$TaskDao extends TaskDao { (Task item) => { 'id': item.id, 'message': item.message, - 'isRead': item.isRead == null ? null : (item.isRead! ? 1 : 0), + 'isRead': item.isRead ? 1 : 0, 'timestamp': _dateTimeConverter.encode(item.timestamp), 'type': item.type.index }, @@ -129,7 +129,7 @@ class _$TaskDao extends TaskDao { (Task item) => { 'id': item.id, 'message': item.message, - 'isRead': item.isRead == null ? null : (item.isRead! ? 1 : 0), + 'isRead': item.isRead ? 1 : 0, 'timestamp': _dateTimeConverter.encode(item.timestamp), 'type': item.type.index }, @@ -152,7 +152,7 @@ class _$TaskDao extends TaskDao { return _queryAdapter.query('SELECT * FROM task WHERE id = ?1', mapper: (Map row) => Task( row['id'] as int?, - row['isRead'] == null ? null : (row['isRead'] as int) != 0, + (row['isRead'] as int) != 0, row['message'] as String, _dateTimeConverter.decode(row['timestamp'] as int), TaskType.values[row['type'] as int]), @@ -164,7 +164,7 @@ class _$TaskDao extends TaskDao { return _queryAdapter.queryList('SELECT * FROM task', mapper: (Map row) => Task( row['id'] as int?, - row['isRead'] == null ? null : (row['isRead'] as int) != 0, + (row['isRead'] as int) != 0, row['message'] as String, _dateTimeConverter.decode(row['timestamp'] as int), TaskType.values[row['type'] as int])); @@ -175,7 +175,7 @@ class _$TaskDao extends TaskDao { return _queryAdapter.queryListStream('SELECT * FROM task', mapper: (Map row) => Task( row['id'] as int?, - row['isRead'] == null ? null : (row['isRead'] as int) != 0, + (row['isRead'] as int) != 0, row['message'] as String, _dateTimeConverter.decode(row['timestamp'] as int), TaskType.values[row['type'] as int]), @@ -188,7 +188,7 @@ class _$TaskDao extends TaskDao { return _queryAdapter.queryListStream('SELECT * FROM task WHERE type = ?1', mapper: (Map row) => Task( row['id'] as int?, - row['isRead'] == null ? null : (row['isRead'] as int) != 0, + (row['isRead'] as int) != 0, row['message'] as String, _dateTimeConverter.decode(row['timestamp'] as int), TaskType.values[row['type'] as int]), diff --git a/example/lib/main.dart b/example/lib/main.dart index 888c5aee..3dfd4ff0 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -57,14 +57,17 @@ class TasksWidgetState extends State { actions: [ PopupMenuButton( itemBuilder: (context) { - return List.generate(4, (index) { - return PopupMenuItem( - value: index, - child: Text( - index == 0 ? 'All' : _getMenuType(index).title, - ), - ); - }); + return List.generate( + TaskType.values.length + 1, //Uses increment to handle All types + (index) { + return PopupMenuItem( + value: index, + child: Text( + index == 0 ? 'All' : _getMenuType(index).title, + ), + ); + }, + ); }, onSelected: (index) { setState(() { @@ -167,8 +170,8 @@ class TaskListCell extends StatelessWidget { direction: DismissDirection.horizontal, child: ListTile( title: Text(task.message), - trailing: Text(task.timestamp.toIso8601String()), subtitle: Text('Status: ${task.type.title}'), + trailing: Text(task.timestamp.toIso8601String()), ), confirmDismiss: (direction) async { String? statusMessage; @@ -180,7 +183,7 @@ class TaskListCell extends StatelessWidget { case DismissDirection.startToEnd: final tasksLength = TaskType.values.length; final nextIndex = (tasksLength + task.type.index + 1) % tasksLength; - final taskCopy = task.copy(type: TaskType.values[nextIndex]); + final taskCopy = task.copyWith(type: TaskType.values[nextIndex]); await dao.updateTask(taskCopy); statusMessage = 'Updated task status by: ${taskCopy.type.title}'; break; @@ -251,7 +254,7 @@ class TasksTextField extends StatelessWidget { if (message.trim().isEmpty) { _textEditingController.clear(); } else { - final task = Task(null, false, message, DateTime.now(), TaskType.open); + final task = Task.optional(message: message); await dao.insertTask(task); _textEditingController.clear(); } diff --git a/example/lib/task.dart b/example/lib/task.dart index 3c37ac2a..267e6d8b 100644 --- a/example/lib/task.dart +++ b/example/lib/task.dart @@ -17,7 +17,7 @@ class Task { final String message; - final bool? isRead; + final bool isRead; final DateTime timestamp; @@ -25,12 +25,27 @@ class Task { Task(this.id, this.isRead, this.message, this.timestamp, this.type); + factory Task.optional({ + int? id, + DateTime? timestamp, + String? message, + bool? isRead, + TaskType? type, + }) => + Task( + id, + isRead ?? false, + message ?? 'empty', + timestamp ?? DateTime.now(), + type ?? TaskType.open, + ); + @override String toString() { return 'Task{id: $id, message: $message, read: $isRead, timestamp: $timestamp, type: $type}'; } - Task copy({ + Task copyWith({ int? id, String? message, bool? isRead, diff --git a/floor_generator/lib/misc/type_utils.dart b/floor_generator/lib/misc/type_utils.dart index 70bd1bfc..8c3f9918 100644 --- a/floor_generator/lib/misc/type_utils.dart +++ b/floor_generator/lib/misc/type_utils.dart @@ -5,7 +5,7 @@ import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/type.dart'; import 'package:source_gen/source_gen.dart'; -extension SupportedTypeChecker on DartType { +extension DartTypeChecker on DartType { /// Whether this [DartType] is either /// - String /// - bool @@ -21,21 +21,11 @@ extension SupportedTypeChecker on DartType { _uint8ListTypeChecker, ]).isExactlyType(this); } -} -extension EnumTypeChecker on DartType { - bool get isEnumType { - final mType = this; - return mType is InterfaceType ? mType.element.isEnum : false; - } -} + bool get isEnumType => _enumTypeChecker.isSuperTypeOf(this); -extension Uint8ListTypeChecker on DartType { - bool get isUint8List => - getDisplayString(withNullability: false) == 'Uint8List'; -} + bool get isUint8List => _uint8ListTypeChecker.isExactlyType(this); -extension StreamTypeChecker on DartType { bool get isStream => _streamTypeChecker.isExactlyType(this); } @@ -70,3 +60,5 @@ final _doubleTypeChecker = _typeChecker(double); final _uint8ListTypeChecker = _typeChecker(Uint8List); final _streamTypeChecker = _typeChecker(Stream); + +final _enumTypeChecker = _typeChecker(Enum); diff --git a/floor_generator/lib/processor/field_processor.dart b/floor_generator/lib/processor/field_processor.dart index 24833a69..8852401b 100644 --- a/floor_generator/lib/processor/field_processor.dart +++ b/floor_generator/lib/processor/field_processor.dart @@ -59,7 +59,6 @@ class FieldProcessor extends Processor { } else if (typeConverter != null) { return typeConverter.databaseType.asSqlType(); } else { - print('DEBUG:: getClosest == null'); throw InvalidGenerationSourceError( 'Column type is not supported for $type.', todo: 'Either make to use a supported type or supply a type converter.',