Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: This package does not work on NullSafety Project #315

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 30
compileSdkVersion 31

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
Expand Down
2 changes: 0 additions & 2 deletions example/lib/location_callback_handler.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import 'dart:async';

import 'package:background_locator/location_dto.dart';

import 'location_service_repository.dart';

class LocationCallbackHandler {
Expand Down
21 changes: 11 additions & 10 deletions example/lib/location_service_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,26 @@ class LocationServiceRepository {
}
print("$_count");
await setLogLabel("start");
final SendPort send = IsolateNameServer.lookupPortByName(isolateName);
final SendPort? send = IsolateNameServer.lookupPortByName(isolateName);
send?.send(null);
}

Future<void> dispose() async {
print("***********Dispose callback handler");
print("$_count");
await setLogLabel("end");
final SendPort send = IsolateNameServer.lookupPortByName(isolateName);
final SendPort? send = IsolateNameServer.lookupPortByName(isolateName);
send?.send(null);
}

Future<void> callback(LocationDto locationDto) async {
print('$_count location in dart: ${locationDto.toString()}');
await setLogPosition(_count, locationDto);
final SendPort send = IsolateNameServer.lookupPortByName(isolateName);
send?.send(locationDto);
_count++;

Future<void> callback(dynamic locationDto) async {
if (locationDto is LocationDto) {
print('$_count location in dart: ${locationDto.toString()}');
await setLogPosition(_count, locationDto);
final SendPort? send = IsolateNameServer.lookupPortByName(isolateName);
send?.send(locationDto);
_count++;
}
}

static Future<void> setLogLabel(String label) async {
Expand All @@ -73,7 +74,7 @@ class LocationServiceRepository {
}

static double dp(double val, int places) {
double mod = pow(10.0, places);
double mod = pow(10.0, places).toDouble();
return ((val * mod).round().toDouble() / mod);
}

Expand Down
71 changes: 37 additions & 34 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:async';
import 'dart:ffi';
import 'dart:isolate';
import 'dart:ui';

Expand All @@ -9,7 +8,7 @@ import 'package:background_locator/settings/android_settings.dart';
import 'package:background_locator/settings/ios_settings.dart';
import 'package:background_locator/settings/locator_settings.dart';
import 'package:flutter/material.dart';
import 'package:location_permissions/location_permissions.dart';
import 'package:permission_handler/permission_handler.dart';

import 'file_manager.dart';
import 'location_callback_handler.dart';
Expand All @@ -26,8 +25,8 @@ class _MyAppState extends State<MyApp> {
ReceivePort port = ReceivePort();

String logStr = '';
bool isRunning;
LocationDto lastLocation;
bool isRunning = false;
LocationDto? lastLocation;

@override
void initState() {
Expand Down Expand Up @@ -56,20 +55,18 @@ class _MyAppState extends State<MyApp> {
super.dispose();
}

Future<void> updateUI(LocationDto data) async {
Future<void> updateUI(LocationDto? data) async {
final log = await FileManager.readLogFile();

await _updateNotificationText(data);

setState(() {
if (data != null) {
lastLocation = data;
}
lastLocation = data;
logStr = log;
});
}

Future<void> _updateNotificationText(LocationDto data) async {
Future<void> _updateNotificationText(LocationDto? data) async {
if (data == null) {
return;
}
Expand Down Expand Up @@ -125,12 +122,10 @@ class _MyAppState extends State<MyApp> {
),
);
String msgStatus = "-";
if (isRunning != null) {
if (isRunning) {
msgStatus = 'Is running';
} else {
msgStatus = 'Is not running';
}
if (isRunning) {
msgStatus = 'Is running';
} else {
msgStatus = 'Is not running';
}
final status = Text("Status: $msgStatus");

Expand Down Expand Up @@ -180,32 +175,40 @@ class _MyAppState extends State<MyApp> {
}

Future<bool> _checkLocationPermission() async {
final access = await LocationPermissions().checkPermissionStatus();
switch (access) {
case PermissionStatus.unknown:
case PermissionStatus.denied:
case PermissionStatus.restricted:
final permission = await LocationPermissions().requestPermissions(
permissionLevel: LocationPermissionLevel.locationAlways,
);
if (permission == PermissionStatus.granted) {
return true;
} else {
return false;
try {
/// Check service
/// Check Permission
PermissionStatus status = await Permission.location.status;
PermissionStatus statusAlways = await Permission.locationAlways.status;
if (statusAlways.isGranted) {
return true;
}
if (status.isDenied) {
/// Request if Denied
status = await Permission.location.request();
}
// At first, make request for foreground location access.
// And then you can request background location access.
if (status.isGranted) {
if (!statusAlways.isGranted) {
statusAlways = await Permission.locationAlways.request();
statusAlways = await Permission.locationAlways.status;
}
break;
case PermissionStatus.granted:
}
if (statusAlways.isGranted) {
return true;
break;
default:
} else {
return false;
break;
}
} catch (e) {
return false;
}
}

Future<void> _startLocator() async{
Future<void> _startLocator() async {
Map<String, dynamic> data = {'countInit': 1};
return await BackgroundLocator.registerLocationUpdate(LocationCallbackHandler.callback,
return await BackgroundLocator.registerLocationUpdate(
LocationCallbackHandler.callback,
initCallback: LocationCallbackHandler.initCallback,
initDataCallback: data,
disposeCallback: LocationCallbackHandler.disposeCallback,
Expand Down
4 changes: 2 additions & 2 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Demonstrates how to use the background_locator plugin.
publish_to: 'none'

environment:
sdk: ">=2.8.0 <3.0.0"
sdk: ">=2.12.0 <3.0.0"

dependencies:
flutter:
Expand All @@ -21,7 +21,7 @@ dev_dependencies:
path: ../

path_provider: ^2.0.8
location_permissions: ^3.0.0+1
permission_handler: ^9.1.0

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
Expand Down
25 changes: 0 additions & 25 deletions example/test/widget_test.dart
Original file line number Diff line number Diff line change
@@ -1,26 +1 @@
// This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility that Flutter provides. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.

import 'package:background_locator_example/main.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
testWidgets('Verify Platform version', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(MyApp());

// Verify that platform version is retrieved.
expect(
find.byWidgetPredicate(
(Widget widget) =>
widget is Text && widget.data.startsWith('Running on:'),
),
findsOneWidget,
);
});
}
2 changes: 1 addition & 1 deletion lib/location_dto.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class LocationDto {
json[Keys.ARG_HEADING],
json[Keys.ARG_TIME],
isLocationMocked,
json[Keys.ARG_PROVIDER],
json[Keys.ARG_PROVIDER] ?? '',
);
}

Expand Down