Merge pull request #17 from k9withabone/null-safety-fixes

Null safety fixes
This commit is contained in:
Benimautner 2022-09-08 21:55:21 +02:00 committed by GitHub
commit 57b219836f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 62 additions and 60 deletions

View File

@ -14,7 +14,7 @@ class Bucket {
final List<Task> tasks;
Bucket({
this.id = -1,
this.id = 0,
required this.listId,
required this.title,
this.position,
@ -47,7 +47,7 @@ class Bucket {
.toList();
toJSON() => {
'id': id != -1 ? id : null,
'id': id,
'list_id': listId,
'title': title,
'position': position,

View File

@ -13,7 +13,7 @@ class Label {
late final Color textColor = color != null && color!.computeLuminance() <= 0.5 ? vLabelLight : vLabelDark;
Label({
this.id = -1,
this.id = 0,
required this.title,
this.description = '',
this.color,
@ -35,7 +35,7 @@ class Label {
createdBy = User.fromJson(json['created_by']);
toJSON() => {
'id': id != -1 ? id : null,
'id': id,
'title': title,
'description': description,
'hex_color':

View File

@ -11,7 +11,7 @@ class TaskList {
final bool isFavorite;
TaskList({
this.id = -1,
this.id = 0,
required this.title,
required this.namespaceId,
this.description = '',
@ -39,7 +39,7 @@ class TaskList {
toJSON() {
return {
'id': id != -1 ? id : null,
'id': id,
'title': title,
'description': description,
'owner': owner.toJSON(),

View File

@ -7,7 +7,7 @@ class Namespace {
final User owner;
Namespace({
this.id = -1,
this.id = 0,
DateTime? created,
DateTime? updated,
required this.title,
@ -25,7 +25,7 @@ class Namespace {
owner = User.fromJson(json['owner']);
Map<String, dynamic> toJSON() => {
'id': id != -1 ? id : null,
'id': id,
'created': created.toUtc().toIso8601String(),
'updated': updated.toUtc().toIso8601String(),
'title': title,

View File

@ -12,15 +12,15 @@ class Task {
final int? parentTaskId, priority, bucketId;
final int listId;
final DateTime created, updated;
final DateTime? dueDate, startDate, endDate;
DateTime? dueDate, startDate, endDate;
final List<DateTime> reminderDates;
final String identifier;
final String title, description;
final bool done;
final Color? color;
Color? color;
final double? kanbanPosition;
final User createdBy;
final Duration? repeatAfter;
Duration? repeatAfter;
final List<Task> subtasks;
final List<Label> labels;
final List<TaskAttachment> attachments;
@ -28,11 +28,9 @@ class Task {
late final checkboxStatistics = getCheckboxStatistics(description);
late final hasCheckboxes = checkboxStatistics.total != 0;
late final textColor = (color != null && color!.computeLuminance() > 0.5) ? Colors.black : Colors.white;
late final hasDueDate = dueDate?.year != 1;
Task({
this.id = -1,
this.id = 0,
this.identifier = '',
this.title = '',
this.description = '',
@ -59,6 +57,14 @@ class Task {
bool loading = false;
Color get textColor {
if (color != null && color!.computeLuminance() > 0.5) {
return Colors.black;
}
return Colors.white;
}
bool get hasDueDate => dueDate?.year != 1;
Task.fromJson(Map<String, dynamic> json)
: id = json['id'],
title = json['title'],
@ -104,7 +110,7 @@ class Task {
createdBy = User.fromJson(json['created_by']);
toJSON() => {
'id': id != -1 ? id : null,
'id': id,
'title': title,
'description': description,
'identifier': identifier.isNotEmpty ? identifier : null,
@ -146,7 +152,6 @@ class Task {
String? identifier,
bool? done,
Color? color,
bool? resetColor,
double? kanbanPosition,
User? createdBy,
Duration? repeatAfter,
@ -170,7 +175,7 @@ class Task {
description: description ?? this.description,
identifier: identifier ?? this.identifier,
done: done ?? this.done,
color: (resetColor ?? false) ? null : (color ?? this.color),
color: color ?? this.color,
kanbanPosition: kanbanPosition ?? this.kanbanPosition,
createdBy: createdBy ?? this.createdBy,
repeatAfter: repeatAfter ?? this.repeatAfter,

View File

@ -10,7 +10,7 @@ class TaskAttachment {
// TODO: add file
TaskAttachment({
this.id = -1,
this.id = 0,
required this.taskId,
DateTime? created,
required this.createdBy,
@ -23,7 +23,7 @@ class TaskAttachment {
createdBy = User.fromJson(json['created_by']);
toJSON() => {
'id': id != -1 ? id : null,
'id': id,
'task_id': taskId,
'created': created.toUtc().toIso8601String(),
'created_by': createdBy.toJSON(),

View File

@ -7,7 +7,7 @@ class User {
final DateTime created, updated;
User({
this.id = -1,
this.id = 0,
this.name = '',
required this.username,
DateTime? created,
@ -23,7 +23,7 @@ class User {
updated = DateTime.parse(json['updated']);
toJSON() => {
'id': id != -1 ? id : null,
'id': id,
'name': name,
'username': username,
'created': created.toUtc().toIso8601String(),

View File

@ -365,19 +365,18 @@ class _TaskEditPageState extends State<TaskEditPage> {
// Removes all reminders with no value set.
_reminderDates.removeWhere((d) => d == DateTime(0));
Task updatedTask = widget.task.copyWith(
final updatedTask = widget.task.copyWith(
title: _title,
description: _description,
reminderDates: _reminderDates,
dueDate: _dueDate,
startDate: _startDate,
endDate: _endDate,
priority: _priority,
repeatAfter: _repeatAfter,
labels: _labels,
color: _resetColor ? null : (_color ?? widget.task.color),
resetColor: _resetColor,
);
)
..dueDate = _dueDate
..startDate = _startDate
..endDate = _endDate
..color = _resetColor ? null : (_color ?? widget.task.color)
..repeatAfter = _repeatAfter;
// update the labels
await VikunjaGlobal.of(context)

View File

@ -1,4 +1,4 @@
getRepeatAfterTypeFromDuration(Duration? repeatAfter) {
String? getRepeatAfterTypeFromDuration(Duration? repeatAfter) {
if (repeatAfter == null || repeatAfter.inSeconds == 0) {
return null;
}
@ -18,7 +18,7 @@ getRepeatAfterTypeFromDuration(Duration? repeatAfter) {
return 'Hours';
}
getRepeatAfterValueFromDuration(Duration? repeatAfter) {
int? getRepeatAfterValueFromDuration(Duration? repeatAfter) {
if (repeatAfter == null || repeatAfter.inSeconds == 0) {
return null;
}
@ -43,13 +43,16 @@ getRepeatAfterValueFromDuration(Duration? repeatAfter) {
return repeatAfter.inHours;
}
getDurationFromType(String? value, String? type) {
Duration? getDurationFromType(String? value, String? type) {
// Return an empty duration if either of the values is not set
if (value == null || value == '' || type == null || type == '') {
return Duration();
}
int val = int.parse(value);
int? val = int.tryParse(value);
if (val == null) {
return null;
}
switch (type) {
case 'Hours':
@ -63,4 +66,6 @@ getDurationFromType(String? value, String? type) {
case 'Years':
return Duration(days: val * 365);
}
return null;
}

View File

@ -42,7 +42,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.8.2"
version: "2.9.0"
boolean_selector:
dependency: transitive
description:
@ -70,14 +70,7 @@ packages:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.1"
version: "1.2.1"
checked_yaml:
dependency: transitive
description:
@ -98,7 +91,7 @@ packages:
name: clock
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.1.1"
collection:
dependency: transitive
description:
@ -168,7 +161,7 @@ packages:
name: fake_async
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.3.1"
ffi:
dependency: transitive
description:
@ -400,21 +393,21 @@ packages:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.11"
version: "0.12.12"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.4"
version: "0.1.5"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.7.0"
version: "1.8.0"
mime:
dependency: transitive
description:
@ -449,7 +442,7 @@ packages:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.1"
version: "1.8.2"
path_drawing:
dependency: transitive
description:
@ -594,7 +587,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.2"
version: "1.9.0"
stack_trace:
dependency: transitive
description:
@ -615,35 +608,35 @@ packages:
name: string_scanner
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.1.1"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.2.1"
test:
dependency: "direct dev"
description:
name: test
url: "https://pub.dartlang.org"
source: hosted
version: "1.21.1"
version: "1.21.4"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.9"
version: "0.4.12"
test_core:
dependency: transitive
description:
name: test_core
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.13"
version: "0.4.16"
timezone:
dependency: transitive
description:

View File

@ -45,32 +45,32 @@ void main() {
});
test('Hours to duration', () {
Duration parsedDuration = getDurationFromType('6', 'Hours');
Duration? parsedDuration = getDurationFromType('6', 'Hours');
expect(parsedDuration, Duration(hours: 6));
});
test('Days to duration', () {
Duration parsedDuration = getDurationFromType('6', 'Days');
Duration? parsedDuration = getDurationFromType('6', 'Days');
expect(parsedDuration, Duration(days: 6));
});
test('Weeks to duration', () {
Duration parsedDuration = getDurationFromType('6', 'Weeks');
Duration? parsedDuration = getDurationFromType('6', 'Weeks');
expect(parsedDuration, Duration(days: 6 * 7));
});
test('Months to duration', () {
Duration parsedDuration = getDurationFromType('6', 'Months');
Duration? parsedDuration = getDurationFromType('6', 'Months');
expect(parsedDuration, Duration(days: 6 * 30));
});
test('Years to duration', () {
Duration parsedDuration = getDurationFromType('6', 'Years');
Duration? parsedDuration = getDurationFromType('6', 'Years');
expect(parsedDuration, Duration(days: 6 * 365));
});
test('null to duration', () {
Duration parsedDuration = getDurationFromType(null, null);
Duration? parsedDuration = getDurationFromType(null, null);
expect(parsedDuration, Duration());
});
}