Skip to content

Commit

Permalink
migrate to null-safety, fix errors after package upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
onur-yildiz committed Apr 24, 2021
1 parent 269991f commit 60005c0
Show file tree
Hide file tree
Showing 29 changed files with 424 additions and 421 deletions.
16 changes: 8 additions & 8 deletions lib/helpers/db_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class DBHelper {
);
}

static Future<void> insert(String table, Map<String, Object> data) async {
static Future<int> insert(String table, Map<String, Object?> data) async {
final db = await DBHelper.database();
return db.insert(
table,
Expand All @@ -30,10 +30,10 @@ class DBHelper {
);
}

static Future<void> update(
static Future<int> update(
String table,
String column,
String id,
String? id,
dynamic data,
) async {
final db = await DBHelper.database();
Expand All @@ -46,7 +46,7 @@ class DBHelper {
String column,
int oldIndex,
int newIndex, [
String groupingColStr = '',
String? groupingColStr = '',
String groupingCol = '',
]) async {
final db = await DBHelper.database();
Expand Down Expand Up @@ -78,7 +78,7 @@ class DBHelper {
);
}

static Future<void> reorderGroupedBy(
static Future<int> reorderGroupedBy(
String table,
String column,
String groupingColVal,
Expand All @@ -101,15 +101,15 @@ class DBHelper {
);
}

static Future<void> delete(String table, String id) async {
static Future<int> delete(String table, String? id) async {
final db = await DBHelper.database();
return db.rawDelete('DELETE FROM $table WHERE id = ?', [id]);
}

static Future<List<Map<String, Object>>> getData(
static Future<List<Map<String, Object?>>> getData(
String table, [
String column = '',
String id = '',
String? id = '',
]) async {
final db = await DBHelper.database();
if (id == '' || column == '') {
Expand Down
53 changes: 27 additions & 26 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
final BehaviorSubject<ReceivedNotification> didReceiveLocalNotificationSubject =
BehaviorSubject<ReceivedNotification>();

final BehaviorSubject<String> selectNotificationSubject =
BehaviorSubject<String>();
final BehaviorSubject<String?> selectNotificationSubject =
BehaviorSubject<String?>();

class ReceivedNotification {
ReceivedNotification({
@required this.id,
@required this.title,
@required this.body,
@required this.payload,
required this.id,
required this.title,
required this.body,
required this.payload,
});

final int id;
Expand All @@ -43,12 +43,12 @@ class ReceivedNotification {
final String payload;
}

String selectedNotificationPayload;
String? selectedNotificationPayload;
// String _initialRoute;

void main() async {
WidgetsFlutterBinding.ensureInitialized();
Workmanager.initialize(
Workmanager().initialize(
callbackDispatcher,
);

Expand All @@ -70,7 +70,7 @@ void main() async {
);

await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: (String payload) async {
onSelectNotification: (String? payload) async {
if (payload != null) {
debugPrint('notification payload: $payload');
}
Expand Down Expand Up @@ -187,12 +187,12 @@ Future<void> _fetchStartupData(BuildContext context) async {

void _setDailyQuoteNotification(BuildContext context) async {
if (Provider.of<SettingsProvider>(context, listen: false)
.receiveQuoteNotifs) {
Workmanager.registerPeriodicTask(
.receiveQuoteNotifs!) {
Workmanager().registerPeriodicTask(
'quote-notification',
'quote-notification',
inputData: {
'locale': AppLocalizations.of(context).localeName,
'locale': AppLocalizations.of(context)!.localeName,
},
frequency: Duration(days: 1),
existingWorkPolicy: ExistingWorkPolicy.keep,
Expand All @@ -205,26 +205,26 @@ void _setDailyQuoteNotification(BuildContext context) async {
),
);
} else {
Workmanager.cancelByUniqueName('quote-notification');
Workmanager().cancelByUniqueName('quote-notification');
}
}

void callbackDispatcher() {
Workmanager.executeTask((taskName, inputData) async {
Workmanager().executeTask((taskName, inputData) async {
switch (taskName) {
case 'progress-notification':
final addictionData =
await DBHelper.getData('addictions', 'id', inputData['id']);
await DBHelper.getData('addictions', 'id', inputData!['id']);
final addiction = Addiction(
id: addictionData[0]['id'],
name: addictionData[0]['name'],
quitDate: addictionData[0]['quit_date'],
consumptionType: addictionData[0]['consumption_type'],
dailyConsumption: addictionData[0]['daily_consumption'],
unitCost: addictionData[0]['unit_cost'],
level: addictionData[0]['level'],
id: addictionData[0]['id'] as String,
name: addictionData[0]['name'] as String,
quitDate: addictionData[0]['quit_date'] as String,
consumptionType: addictionData[0]['consumption_type'] as int,
dailyConsumption: addictionData[0]['daily_consumption'] as double,
unitCost: addictionData[0]['unit_cost'] as double,
level: addictionData[0]['level'] as int?,
);
final nextLevel = addiction.level + 1;
final nextLevel = addiction.level! + 1;
if (addiction.abstinenceTime.inSeconds >=
levelDurations[nextLevel].inSeconds) {
await DBHelper.update(
Expand All @@ -240,11 +240,11 @@ void callbackDispatcher() {
}
// if last achievement level, cancel
if (addiction.level == 8) {
Workmanager.cancelByUniqueName(addiction.id);
Workmanager().cancelByUniqueName(addiction.id);
}
break;
case 'quote-notification':
final quoteList = quotes[inputData['locale']];
final quoteList = quotes[inputData!['locale']]!;
Random random = new Random(DateTime.now().millisecondsSinceEpoch);
int rndi = random.nextInt(quoteList.length);
showQuoteNotification(
Expand Down Expand Up @@ -276,7 +276,8 @@ void showProgressNotification(
);
}

void showQuoteNotification(String notificationTitle, String notificationBody) {
void showQuoteNotification(
String? notificationTitle, String? notificationBody) {
final androidDetails = AndroidNotificationDetails(
'quitAllProgress',
'progressNotifications',
Expand Down
30 changes: 15 additions & 15 deletions lib/models/addiction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ class Addiction {
final int consumptionType;
final double dailyConsumption;
final double unitCost;
List<PersonalNote> personalNotes;
List<Gift> gifts;
int level;
int achievementLevel;
int sortOrder;
List<PersonalNote>? personalNotes;
List<Gift>? gifts;
int? level;
int? achievementLevel;
int? sortOrder;

Addiction({
@required this.id,
@required this.name,
@required this.quitDate,
@required this.consumptionType,
@required this.dailyConsumption,
@required this.unitCost,
required this.id,
required this.name,
required this.quitDate,
required this.consumptionType,
required this.dailyConsumption,
required this.unitCost,
this.personalNotes,
this.gifts,
this.level,
Expand Down Expand Up @@ -56,8 +56,8 @@ class Addiction {

double get totalSpent {
double total = 0.0;
gifts.forEach((gift) {
total += gift.price * gift.count;
gifts!.forEach((gift) {
total += gift.price! * gift.count!;
});
return total;
}
Expand All @@ -67,9 +67,9 @@ class Addiction {
}

List<PersonalNote> get personalNotesDateSorted {
List<PersonalNote> list = [...personalNotes];
List<PersonalNote> list = [...personalNotes!];
list.sort((a, b) {
return DateTime.parse(b.date).compareTo(DateTime.parse(a.date));
return DateTime.parse(b.date!).compareTo(DateTime.parse(a.date!));
});
return list;
}
Expand Down
24 changes: 12 additions & 12 deletions lib/models/gift.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import 'package:flutter/foundation.dart';

class Gift {
final String id;
final String addictionId;
final String name;
final double price;
int sortOrder;
int count;
final String? id;
final String? addictionId;
final String? name;
final double? price;
int? sortOrder;
int? count;

Gift({
@required this.id,
@required this.addictionId,
@required this.name,
@required this.price,
@required this.sortOrder,
int count,
required this.id,
required this.addictionId,
required this.name,
required this.price,
required this.sortOrder,
int? count,
}) {
this.count = count ?? 0;
}
Expand Down
12 changes: 6 additions & 6 deletions lib/models/personal_note.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import 'package:flutter/foundation.dart';

class PersonalNote {
final String title;
final String text;
final String date;
final String? title;
final String? text;
final String? date;

PersonalNote({
@required this.title,
@required this.text,
@required this.date,
required this.title,
required this.text,
required this.date,
});
}
Loading

0 comments on commit 60005c0

Please sign in to comment.