I have a boolean bool isDriverAvailable=false which will check if the user is online or not, if it is online then the GeoFire plugin will update its latitude and longitude to the database.
I am using a background location for this project which has a callback
static void backgroundLocationCallBack(LocationDto location) method where I plan to implement this check and update the location.
this is my code so far
class _HomeTabPageState extends State<HomeTabPage>
with AutomaticKeepAliveClientMixin, OSMMixinObserver {
bool isDriverAvailable = false;
//some code
static void backgroundLocationCallBack(LocationDto location) async {
print("from background location +${location.latitude}");
if (isDriverAvailable)
Geofire.setLocation(
currentFirebaseUser!.uid, location.latitude!, location.longitude!);
}
void getLocationLiveUpdates() async {
return await BackgroundLocator.registerLocationUpdate(
backgroundLocationCallBack,
autoStop: false,
// disposeCallback: backgroundLocationDisposeCallBack,
androidSettings: AndroidSettings(
accuracy: LocationAccuracy.NAVIGATION,
interval: 5,
distanceFilter: 0,
client: LocationClient.google,
androidNotificationSettings: AndroidNotificationSettings(
notificationChannelName: 'Location tracking',
notificationTitle: 'Start Location Tracking',
notificationMsg: 'Track location in background',
notificationBigMsg:
'Background location is on to keep the app up-tp-date with your location. This is required for main features to work properly when the app is not running.',
notificationIconColor: Colors.grey,
)));
}
I get an error on if (isDriverAvailable)
The error
Instance members can't be accessed from a static method.
Try removing the reference to the instance member, or removing the keyword 'static' from the method.
I can't remove the static keyboard on backgroundLocationCallBack(LocationDto location) as it is only meant to work with static method.
How else can I implement the bool check ?
You will need to somehow pass the isDriverAvailable boolean to your function, remove the static from the method, or refactor the whole thing.
Static members of a class need to be available from a class-level (in comparison to usual members being available on instance-level), which means that you don't get the implicit this member, as it is assumed you will call the function from the outside the instance.
I gave a quick glance to the background_locator package and its documentation, but nothing states this callback needs to be static, as long as it takes a LocationDto argument and returns void.
Related
I'm trying to add code to my Flame Game to check if a list isn't empty and if it isn't, then send it to a function. However, I'm receiving an error on the if statement that says "Expected an identifier". How do I change my code to run an if statement here? Additionally, how would I cancel the Timer after it runs?
var instructions = [];
myGame(){
add(
TimerComponent(period: 2, repeat: true, onTick: () =>
if(instructions != null){populateInfo(instructions)}),
);
}
You can use runtimeType, Use runtimeType to get the runtime type.
A property of the Object class, which is the base class of all objects in Dart, and of type Type.
for (final element in gameRef.children) {
if (element.runtimeType == Instructions) {
//my element exist in the scene
}
}
I used the last code for explain the use of runtimeType, but with Dart you have more options like to
children.query<Intructions>();
I am following this plugin (learning) background locator . I have previously used other background location plugins but they are not maintained anymore.
I am following the tutorial as described in the description of the plugin and so far this is my code.
I am unsure about what exactly a callback is and how do I use it.
This is my code so far but I don't get any results.
static void backgroundLocationCallBack(LocationDto location) async {
await BackgroundLocator.initialize();
await BackgroundLocator.isServiceRunning();
}
void getLocationLiveUpdates() async {
return await BackgroundLocator.registerLocationUpdate(
backgroundLocationCallBack,
autoStop: false,
// disposeCallback: backgroundLocationDisposeCallBack,
androidSettings: AndroidSettings(
accuracy: LocationAccuracy.NAVIGATION,
interval: 5,
distanceFilter: 0,
client: LocationClient.google,
androidNotificationSettings: AndroidNotificationSettings(
notificationChannelName: 'Location tracking',
notificationTitle: 'Start Location Tracking',
notificationMsg: 'Track location in background',
notificationBigMsg:
'Background location is on to keep the app up-tp-date with your location. This is required for main features to work properly when the app is not running.',
notificationIconColor: Colors.grey,
)));
}
Callbacks are used to perform some actions during certain events.
Here, the callback attribute takes backgroundLocationCallBack as its value, and as per the package code, it is used for performing appropriate actions when the LocationDto is available. This LocationDto object will contain the Location information you are looking for, and you can use it according to your use-case.
Similarly, disposeCallback is used to perform certain actions when dispose is called on the BackgroundLocator object. You can do something like close all the Ports that were opened for fetching the location.
There are other callbacks as well such as initCallback and initDataCallback which are used for setting the callbacks to initialise various parameters for your location service and to provide an initial data for the params respectively.
I was documenting my code and I wanted to link another method in the same file
I want to refer to that method in a comment so anyone reading my comment can directly jump to that method where my comment refers
Method where I am documenting and I want to link to a method named toggleFavorite in the same file
// To avoid code duplication in [toggleFavorite] method
void _toogleFav(newStatus) {
isFavorite = newStatus;
notifyListeners();
}
This is what my toggleFavorite returns in case you need it
Future<void> toggleFavorite() async {
What I want
Exactly I want this word in my comment [toggleFavorite] to work as a link when I press this I get redirected to this method wherever it is created or used
Use triple /// and [] around the method
/// To avoid code duplication in [toggleFavorite] method
void _toogleFav(newStatus) {
isFavorite = newStatus;
notifyListeners();
}
I want to test a method that is responsible for a button tap (let's call it onButtonTap()), one of the first methods is a call to static method from utils file, that returns true of false, depending on set android/ios permissions (or allows user to change permissions by showing dialog that can open application settings). Let's call it checkOrRequestPermissions(). This makes everything behind that code untestable, as I don't know how to test it - I can't mock this class because:
It's not injected anywhere - it's inside utils file
It's static
So for better visualization lets go like this:
Code from file I want to test:
Future<void> onButtonTap(BuildContext context) async {
bool isGranted = await PermissionsUtil.checkOrRequestPermissions([some_args]);
// CODE_A - some code I want to test
}
Code inside PermissionsUtil:
class PermissionsUtil{
static Future<bool> checkOrRequestPermissions([some_args]){
// code for permissions
}
}
So my questions are:
Is there any way I could mock checkOrRequestPermissions() to simply return given value?
How could I make this code testable?
I have an issue which I couldn't figure out for hours,
I have a fragments inside an activity, and sometimes I call the fragment with the codes below:
newsFeedFragment fragment = new newsFeedFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.hide(getSupportFragmentManager().findFragmentById(R.id.fragment_container));
fragmentTransaction.hide(getSupportFragmentManager().findFragmentByTag("notifications_fragment"));
fragmentTransaction.show(getSupportFragmentManager().findFragmentByTag("news_feed_fragment"));
fragmentTransaction.addToBackStack(null);
fragment.onResume();
fragmentTransaction.commit();
As the onResume is not called while showing the fragments I use "fragment.onResume();" in the code below. And when the fragment is shown, the onResume is called. However, I try to update a variable in the onResume method, but it is not updated with the code below. When ever the onResume is called, I see "1" as the result in the log, however I was expecting it to increase by 1 every time. Is there a way to make it work?
int refreshNotificationVar = 0; //in the main class
#Override
public void onResume(){
super.onResume();
refreshNotificationVar = refreshNotificationVar + 1;
System.out.println(refreshNotificationVar);
}
You cannot rely on instance variables in case of onPause and onResume; you can rely on static variables to some extent; you can use onSaveInstanceState; or use a Singleton class to store variable values; or store in shared preferences; or maybe store in a database depending on your needs. In your case, I would use a Singleton class to store the values and get/set them in onPause/onResume.