All your material and website is REALLY exceptional. The video is breakneck speed for people like me who are just starting out, but the step by step on the website is totally spot on to recap when I get lost. Other instructional vids go down unrealistic 'non real world' examples and teach poor design habits. However, just by watching your videos I've taken a step back from learning the small picture stuff and am now focused on understanding bigger picture design concepts. I'll fill the knowledge gaps in slowly as I go. Thanks so much for the terrific quality. You've helped a lot.
Mark! This is a very great comment and I really appreciate it! Thank you for taking the time to write it, I'm very happy I can help you with that. If you want to understand why I write code in a certain way I can give you some things to read.
Number one. SOLID principles, single responsibility is the reason I create services and use MvvM as my architecture.
Number 2: The pragmatic programmer: From journey man to master. Ideas and thoughts on building maintainable more robust software
Number 3: Clean code.
That should put you right where you want to be. And then any talk by Robert C Martin or Martin Fowler.
@@FilledStacks Excellent thanks mate. I'll check those guys out. I already started to check up on SOLID after you mentioned single responsibility on the GPS website blurb. Found a good series on youtube, albeit in C#. Back-tracing the GPS plugin to understand what/how you were doing after I manually duplicated the code taught me more about design than 10 hours of video watching endless int/var/class bs. Even to an amateur eye, Flutter has got to be the last framework in the world you'd want to be bad at keeping clean! Thanks again man.
@@Mark-mo4bo Aweeesome!!! you're very welcome man I'll always put principles over the topic because that's the main thing that keeps my code maintainable. welcome to the club, you'll learn a lot. please comment and let me know if anything could be done better in my videos or you have suggestions for me.
@@FilledStacks Hey Dane just throwing it out there.. If you're interested and had the time, how many rand an hour would you charge for a bit of 1 on 1 programming mentoring / screen sharing? There's always a big 'but why' gap with programming between the simple stuff and serious stuff where the serious stuff becomes too much copy and paste without understanding the mechanisms (and thus being able to apply to other concepts). Let me know if you're interested mate. Cheers, Mark.
@@Mark-mo4bo Hey Mark. that's very true, I try to explain most of what i do but i can't always say everything in every video so it makes it harder to get the message across. especially if you don't watch all the previous videos. On your request, I currently won't have time for that and I also wouldn't feel comfortable charging an individual the amount that i would want in order to do it. Companies pay it and I'm fine with it, but not individuals. If you want to understand why I do things in code the way i do i recommend learning the SOLID principles (especially the single responsibility principle) and reading clean code. from there you can read a book called Pragmatic Programmer : From journeyman to master. And then watch as much robert c martin and brett fowler talks as you can 🤓 It'll give you the idea behind my mindset, why i so certain things and Why i prefer it that way as well.
You're the greatest, I was looking a vieotutorial and the yours is the best.
It makes me very happy to hear that! Thank you so much for watching and the positive feedback.
Great tutorial. No need to use Scaffold, you can use any Widget with Material Theme, such Material widget
Great tutorial!
Could you show us how to make a qr code scanner?
There are severel packages out there but they are not working or way to complex to use
That's a very specific use case so won't go into that unfortunately. I want to focus more on broader concepts and not specific implementations.
im getting Widget Function(BuildContext)' can't be assigned to the parameter type 'Widget Function(BuildContext, Widget) for StreamProvider(
builder: (context) => LocationService().locationStream,
You're supplying the wrong type to the function. I think the name has changed to create for that property as well.
@@FilledStacks figured it out, turns out things have changed since you made this app, StreamProvider does not require builder.
return StreamProvider.value(
initialData: UserLocation(),
value: LocationService().locationStream,
child: MaterialApp(title: 'Flutter Demo', home: HomeView()));
@@ryandooley4819 Good to know you figured it out. Builder has been renamed to create, the .value constructor won't dispose your stream but you can use it for providing a stream that you'll re-use. If that's what you're looking to do
nice tutorial... love the way you explained , what if application is in background or removed from background
Thank you. If the app is in the background it'll work. If it's removed from the background the service will stop. It can be restarted by your app but I have a feeling with new OS's rolling out that's going to become even more difficult.
I did as you instructed in the video. At 7:13 I found that 'builder' isn't working the same way with latest version of provider. With some reading on provider's page on pub.dev, I replaced it with:
create: (context) => LocationService().locationStream,
This wiped the error off my screen. Can you check and confirm if this will work the same?
Thanks a lot!! I would like to show a video with "Theme dark and Theme Light".
There are a few good ones. Check on resocoder I think he created one. If there's not let me know and I'll plan one.
If I wanted to make an app that doesn’t transfer location data while the person is driving, would I then redo this model but take out the stream controller ?
The scenario doesn't change the implementation in this case. If you don't want updates in real time don't listen to the stream. Request the update from the service when you need it and use it where it's required.
how to draw live location using flutter. I am currently working on a fitness app wherein we track the user location in the background. I would like to know how to draw the location traveled by the user. Can anybody please help me with this problem.
Thank you for reading.
On each update you get from the location changed store the coordinates in a list of coordinates then draw curved lines between them. Or if you can map it to an existing path that would be better. Then you can approximate the path/roads taken and highlight that on your map
How would you use the location service along with a view model for a mapview?
I'd get the location service in the ViewModel and register the listener in an initialise function called from onModelReady. That listener would update a private location position and call notifyListeners. In the view when my builder is called I'd remove the current marker, create a new one with the new position and place it on the map. Or update the old marker if I kept a reference.
Can you produce a video that shows how this concept can be applied to live tracking like the uber taxi type?
Thank you and Great video btw
Thanks. It won't be a long video. All you do is inject your service in the viewmodel or block that controls your map position. subscribe to the location stream and in the listen handler update your position and refresh the state.
Awesome video! Is there a way to extract details of an address from the latitude and longitude? Say you want to find a city or an area. How could this be done and updated? Cheers!
Thanks. Yes there is, you can use the Google places package and get the closes place using the lat/long then get the city and other details from that.
is possible call the latitude to a variable to save on database? how do i call the stream in another class?
It is possble. You call the stream in another class the same way you called it in this class where it's being called now.
i have this error
I/flutter ( 3664): The following NoSuchMethodError was thrown building HomeView(dirty, dependencies:
I/flutter ( 3664): [InheritedProvider]):
I/flutter ( 3664): The getter 'latitude' was called on null.
I/flutter ( 3664): Receiver: null
I/flutter ( 3664): Tried calling: latitude
I/flutter ( 3664): User-created ancestor of the error-causing widget was:
I/flutter ( 3664): MaterialApp
package:mob_loc/main.dart:15
I/flutter ( 3664): When the exception was thrown, this was the stack:
Make sure you supply a stream provider with an initial value. Or guard against the null for your data
Hey, great video but when implementing i am having a NoSuchMethodError: invalid member on null: 'latitude' before giving permission in flutter web. Please help. Thank you in advance
Thank you, sounds like you're trying to check latitude before there's any value. Check if it's null before using it
Hey, what if the user rejects the location. Currently, I am trying to setup location boundaries for the WebApp. I am using a combination of Streams(Mainly inspired by your network connectivity and Streams tutorial) and this tutorial.
Problem: If the user rejects the location I currently have no way to get permission again. Also even if the user closes the tab my service is still left out at the denial stage and it does not change.
HOW I AM Implementing it?
The Client mainly sends the requests in the web application.
Here is the setup:
enum ServiceLocation { present, absent }
StreamController locationServiceController =
StreamController.broadcast();
And then using your tutorial and a calculation that gives us the serviceable area by using haversine formula(ref link: blanq.io/haversineFormula) and an if condition comparing the values returned from the calculation and the boundary set.
Solutions i tried?
I added gettingLocation for requesting the location again but i am getting a obejct error.
class LocationTracking {
Future gettingLocation() {
try {
location.requestPermission().then((granted) {
if (granted == PermissionStatus.granted) {
location.onLocationChanged.listen((changedLocationData) {
if (changedLocationData != null) {
double distance = calculateDistance(
lat2: changedLocationData.latitude,
lon2: changedLocationData.longitude,
lat1:
17.5169, //These are hardcoded values of restaurent given here.
lon1: 78.3428);
//I am currently setting the location boundary to be 120km here.
//Should check on real devices for sure before confirming.
if (distance < 120) {
return ServiceLocation.present;
} else if (distance > 120) {
return ServiceLocation.absent;
}
}
});
} else if (granted == PermissionStatus.denied) {
return ServiceLocation.absent;
} else if (granted == PermissionStatus.deniedForever) {
return ServiceLocation.absent;
}
});
} catch (error) {
print(error);
}}}
here is my call:
FlatButton(
onPressed: () async {
// Location location = Location();
var t = await LocationTracking()
.gettingLocation()
.then((value) => locationServiceController.add(value));
// if()
// print(t);
// locationServiceController.add(t);
},
child: Text("Try again"))
Thank you for your work
Hello and thank you for the well explained video! I need your advice, for a project i need the most accurate approach of getting the user location information. i followed your instruction and recognized later in my car a little delay getting updated information. Is it possible in flutter to combine gps with network information? Thank you!
Hello, you're welcome and thank you for watching. The location is broadcast I think every 5 m of difference. The package contains some accuracy settings that I can't think of right now. If you want to be more accurate you won't get any "real-time" updates. The best thing would be to check how fast you're moving and manually check the location to broadcast. I can't think it through right now but there should be some things you can du using the accelerometer in combination with the gps.
One more question i facing the Problem that requesting the device Position freezing the screen so i believe to work with an backgroundservice?
Thank you for this tutorial. Since you're asking for 'NSLocationAlwaysUsageDescription', will the location updates keep coming as long as the app is in memory?
Hello! First of all, thank you for this video, it helped me a lot with my app. However, I need one information, how can I get the location for each 30 seconds? I need just one latitude and longitude in this time, but, using the onLocationChanged, I receive many locations in just one second, I tryied already to change settings but I still receive many locations, I thought to use a dart function to run all the time just calling the getLocation() but I don't know how can I make it. Could you help me?
Hey William, I'm happy to have helped. You can do exactly as you said. Create a timer and onTick you can call getLocation and broadcast that.
how about trying using async await in the function using Future.delayed(seconds: 30) and then adding location data to the stream.
Hi bro , any solution on event handling when the user denies the location access and app crashes... exception block didn't work in permission
Hi,
Amazing tutorial.
But I'm getting an error 'uri_does_not_exist'
for user_location.dart
Is there any solution?
Thank you. I don't know. I've never seen that error. You'll have to debug to figure out what's wrong.
hello buddy, first of all thank you for this beautiful tutorial... quick question, when requesting the persmission the granted value has to be a bool in order to work in the following IF statement. any quick tip how to go around it?
thanks again
Get around what? Not using a boolean? If so then you can use the status of the permission and compare it to the status that you want it to be.
Hi,
nice video, pls let me know if you are available for custom development, we need to build a simple location tracker for multiple users, similar to this . thanks
Yes, we are available. You can email me at dane[AT]filledstacks.com and discuss further
Thanks...How about next tutorial is show lat long in google map based on this tutorial?
How can you achieve this using StreamBuilder ? and how to create periodic streams ??
Instead of using Provider.of with the stream supply the stream to the stream builder and it should have the same effect
I wants to get location even my app is closed can you please suggest me that how i can implement that? TIA
Register a background handler and start the location tracking on a service for Android.
Super great video. How do you handle if user denies permission. Can we somehow execute the streamprovider thing after listening to the status when it has been changed to authorzed after user initially denied permission?
Thank you 😊 If user denies we don't execute anything. Not much we can do about that.
This will run at statrtup so if the uset has changed the permissions it'll simply activate.
I have everything working properly, however when the map is loading i get this error for 2 seconds:
Error: the widget full screen map tried to read provider but the matching provider returned null. to fix the error, consider changing provider to provider.
I've tried the suggested method and it doesn't work. I have also exhausted almost all the "solution" on stack overflow n no luck.
Again, i am able to get the continuous update, but before the UI is showing, That Red Error Screen pops up and then goes away after 2 seconds and everything works fine afterwards.
Any wizard willing to help this noob? Thank you so much in advance guys.
Happy to hear this still works after so many years. You should provide an initial value in your provider with an empty UserLocation.
How to get current location with google maps so that I can send to any user?
You don't get the current location with google maps you get it with the location package. If you want to open it in google maps for the user then use the google app's uri scheme to open the location using an intent on android and openWebPage on ios.
The interesting part here is that you used StreamProvider instead of providing the whole Service (like what we do in MVVM approach).
So in MVVM we create a ViewModel which extends ChangeNotifier and when we have a change - we call notifyListeners() method.
But approach with StreamProvider - is a bit different. Here we can have multiple streams in one ViewModel sending data to UI, we don't even need to extend ChangeNotifier.
Could you please elaborate on the difference of these two approaches (even though we use Provider package in both of them) and when should we use the first (updating UI with the entire our ViewModel) and the second (having multiple streams in our "ViewModel", even though it may not extend ChangeNotifier)
Thank you
Yes you're right. I mention in the tutorial that I usually have a viewmodel that I use my services with. If you watch my provider v3 architecture you see that I have something called UIproviders, or something along those lines. This is for information that is required on almost every view. I've since moved that into a base viewmodel instead but at the time i liked the shorthand of the stream provider, managing the stream itself and making sure it's unsubscribed. It's just another way of doing it. Either you put data that will be required on every view in a base viewmodel and write duplicate code everywhere to access it. Or you provide it a stream provider once and access if using the duplicate code of the Provider.of call. Both has the same result, it's up to you which one to use. To keep consistent I'd say go the viewmodel route and have a base class with the shared value.
hello great tutorial ! but please i am getting an error on my main.dart file, after writing builder: (context) => LocationService().locationStream, I get this error:>>>> The argument type 'Widget Function(BuildContext)' can't be assigned to the parameter type 'Widget Function(BuildContext, Widget)'
I'll answer the question but you should try and understand these error messages otherwise your development experience won't be great. The answer is to put another parameter in your anonymous function (context, child) => LocationService().locationStream.
The reason for that is simple, as explained in the error message. Lets break it down how I read error messages.
The argument type 'Widget Function(BuildContext)' can't be assigned to the parameter type 'Widget Function(BuildContext, Widget)'
So currently you're giving the builder a function that takes a BuildContext and returns something like this.
builder: (context) => LocationService().locationStream
It's telling you that that's not the same as a function that takes the BuildContext and a Widget and returns a widget. Which is true.
So if your function is doing this builder: (BuildContext context) => LocationService().locationStream and the only thing they're asking is to supply another parameter in your anonymous function then you should simply add another parameter in your anonymous function like this.
builder: (BuildContext context, Widget child) => LocationService().locationStream
Understanding these things early will make it so you can stand on your own feet as a developer. These aren't even development problems, it's compiler problems. Those should always be solvable by you. You can navigate to any definition in Flutter code and see what it expects. It's open source. You should make use of that.
Yes you are right, I am only new to all this, I am just trying to build something to help me learn fast
sir please help me how to update location on sever with http in background
When the location is broadcast from your service like in the video call http.post('yourUrl', body: {'lat': yourLat, 'long': yourLong}); That should do it.
thnx a lot. i need to know how to stop the location updates on press the button
You're welcome :) Cancel the stream subscription you're listening to should stop all updates
sir please i can't see it work when turn off the screen. app still work foreground
Yeah, if you lock the screen you can't see what's on it :) ... You should use a background service if you want the same functionality to run when the app is in the background.
Is it possible by subscribing to the location change we get an error in case the user desactivated the location services to display a message and when it reactive it the message dissapear ? Because when I try to do that I just don't get any location update instead of an error
It's possible from the plugin stand point but you can do that within the service. You'll have to use a timer to check availability of the service. When the user has not given permission you send out your error. When there's permission you cancel the timer , send out a "Permission granted"and then start emitting your actual location.
@@FilledStacks Oh okay thank you for your response I didn't thought you would reply this quickly. So that mean I need to constantly check for the Location enabled/granted and also use the Stream to update the map ? I thought the stream from Geolocation (and not geolocator) was a Timer that check everything because the the stream return every error possible pub.dev/packages/geolocation#handle-location-result but I never got any except if I start the app without having the GPS activated. I will try to check if the location is enabled every seconds or more but since the app should run as a service it will possibly eat more battery
@@RomanLeBg Yeah, once you get the permission accepted you should stop the timer as mentioned above. It'll only run when you don't have permission for location. If they disbale the permission you can listen for that and start the timer again. This way it won't take more battery than the normal implementation since it's a map value check to see which permissions are enabled.
@@FilledStacks I mean when I listen to the location update I don't have any error when I desactivate the GPS in android, I just don't have update maybe I need to create a shared value between the stream and a timer that run every seconds so for every location update I set the value to 10 and the timer decrease it by one every seconds so if I don't have any update for 10 seconds I check if the service is enabled but its a hacky solution. I tried the location library but I don't have any error too. I tried searching some app that needs the GPS but I haven't found a solution yet. Do you have any example or stuff like that ?
@@RomanLeBg I don't. I'd probably do what you're suggesting with checking if there's been no updates for 10 seconds.
Is that background service? if app is closed will it be able to send location to API.. just like background services in native works?
If the app is completely closed then no, but you can spawn an background isolate that can react to the location and trigger an api call. There's a good tutorial for it on medium that uses beacons.
@@FilledStacks Are you saying that the streamcontroller will continue to receive location updates when the app is minimized (with the screen off)?
@@niclas2837 It could. Otherwise you can use a background isolate to achieve that if it's not currently doing it.
Great Tutorial....
Can you add the implementation using markers and google maps ?
Thanks. I might add that next year some time. All you have to do is subscribe to the stream and updste your localarker position in the listen callback function.
Awesome tutorial, thanks! One question before I code it up, if the screen turns black from inactivity, does this service continue to get your location or does it wait to be "awoken"?
Thank you, and good question. It'll continue running until the app is closed or made inactive by the OS.
@@FilledStacks Thanks. Do u know a way to passively get ur location while the app is inactive?
@@MrDacheetz Yes, you have to use a Background service in Android and a Long running action in ios. There's packages for that. You can get it once every 30 minutes I think. I think that's currently the limit. At some point they will probably make it impossible. It's been getting harder and harder with every release to do background work.
@@MrDacheetz Read up on Android Sticky Background services and iOS long running processes and see if you can use it to you advantage.
Can this be used for multiple users tracking?
Yes, it's the same things. just send a user model with an id and update positions based on that id.
@@FilledStacks thanks for replying. I want to use it on my buddypress website, will it work?
i can't use classpath 'com.android.tools.build:gradle:3.3.0'
Then you should probably use a later or older version. Whichever you need to. If you can't upgrade to Android X the go back to a version of the plugin where it didn't use AndroidX
Hey how can I turn on GPS if it's turned off by default. By giving permission It just asking for permission. Not turn it on.
You can't do that unfortunately. You're not allowed to override any option that the user has set out of their own free will. That would introduce a lot of unwanted behaviour from certain apps that you don't want to use the GPS with.
@@FilledStacks I'm talking about its like google maps. If our gps is turned off and we open Google maps thn it gives a Alert dialogue where we click "yes" thn it turn on gps for us
Thank you so much! It helped me.. btw, in the end of the video, your video thumbnail make it closed, and i want to see the result 😭
Thanks man, it's my pleasure to share it. haha, I'm sorry about that. It's not very interesting, just some text updating :)
You are awesome!! Thanks for the videos. I'm learning a lot. I have a topic to suggest bottom navigation bar using routes. Do you already have one?
Thanks man. I appreciate the feedback. I don't have a topic for that but it's a good one. Thanks for the suggestion.
how to get location when app is closed or cleared from recents?
Us the background location handler. The OS won't allow that unless it's been allowed by user, even then you can get it every 50m I think.
is this possible to use in indoor navigation?
That's device dependant. If your phone sends updates in doors then it'll work.
Is it possible to get device velocity with location class???
Yes, calculate the delta from previous location update to latest one and make sure it refreshes with the smallest amount you can. I think every 10 meters or every 5 is possible.
@@FilledStacks does it update location when app is killed or in background??
thanks man it's really a very good video and amazing job
but cat you give me the link of the provider.dart or what exactly in side it
i searched in google provider github repo and couldn't find any thing :(
If anyone runs into problem with AndroidX migration please follow this guide.
flutter.dev/docs/development/packages-and-plugins/androidx-compatibility
Please can you make a video on how to retrieve double value or geo point from firebase
I won't make a video for something so small. Store it in a map with key lat and long and then get it out of the map on device
@@FilledStacks I stored it in firebase but when I am trying to retrieve it in my device it say double value is not a string.
I tried to retrieve double value but I can't
Can you tell me how to retrieve double value from firebase or geo point
Or can you make a small helping clip.
@@chinmayagarnaik6458 That makes sense. You probably have to cast it to what you expect it to be.
when i minimize the app , it wont getting the location
@@FilledStacks how can i get location update continous when app is in background
How to stop the service of location package
Create a function called stop on the service and call that where you need to call it.
Great Tutorial man :) Will this work in the background when user turns the screen off??
Thanks 😁 When the user turns the screen off, yes, when the user closes the app completely, no. You'll need to add that functionality.
FilledStacks wow thanks. I could live with this working on background when user turns screen off. Previously thought I need to write native code for it to work in background. Gotta try this out
@@ksk235 Yeah, unless the OS kills the app it will continue running in the background.
hi, i've tried to see if it works in the background. It doesn't. If I turn off the screen or hit home button to minimize the app the service just stops. I can be sure of that because the location icon also disappears from the task bar as soon as I go to home screen without closing the app. Also in my implementation i've set it to save the updated lat & lng in a backend server whenever I get new location update, the app also stops saving the location in the server when i go to home screen or turn the screen off. I guess there's no way to run it in the background other than writing OS specific code for both android and IOS.
@@FilledStacks this is exactly what i am looking for .
i mean when the app is completely closed , for example after device restart .
something like windows services ...
any idea or hint on how this can be implemented please ?
really appreciate your help on this one .
thanks anyway for this excellent and "to the point" tutorial
can u give the provider.dart code
i can not. it's in the provider library. google provider github and you'll find the repo.
hi,
thank you for this guide.
i get error in this line: if (granted) {
error i get: conditions must have a static type of bool...
the android studio tell me to add != null
what do you think?
the value for granted is of the type PermissionStatus for you want to use (granted == PermissionStatus.granted)
this technique also works when the app will be in background?
for background updates you'll have to start the service using an Android service and do some additional work to wake up the app from the background if you need updates.
@@sasaharukh8575 I'll think about doing that as I'm planning what I want to cover next.
@@FilledStacks please do this! you're fairly easy to follow and we would truly benefit from the hoped-for video, regarding how to get location while the app is not open.
@@aksanabuster1874 Hey, there's a package now that does that which you can use.
If the user allows it it'll work, if their battery settings doesn't turn off background data then you can get it as well. But it's a fleeting feature, it'll soon get banned completely on ios and Android. Or no one will allow it.
So stream is alternative to rxdart
No, rxdarts is built ontop of streams. RxDart you use for use cases more advanced than streams. It's streams++ basically.
@@FilledStacks I have seen people implementing rxdart using Bloc.. can we implement rxdart using provider?
@@bibash3128 you mean people implemented BLoC using RxDart?
Provider itself is a Dependency Injection lib, if you want to use it you can impement MVVM pattern with it
how to calculate distance covered in this code
There's a calculation to get distance between two coordinatea. You can use that one.
How can we run this in background?
Start it as a Background Sticky service in Android. On iOS You'll have to enable background services and use a long running task.
@@FilledStacks Could you break it down a bit? or can you point out a reference?
I can not. I would google Sticky service in Android and Long running task in ios and then go from there.
I think there is no reason to create a model.
Well then you probably shouldn't 😊 never do things if you don't need to 😬
Thanks for this ariticle, It was really helpful for me.
So, I have one question about marker on google map.
First, Please refer this UA-cam link.
ua-cam.com/video/dq-ksuP0zEc/v-deo.html
Under this video, as you see, Marker moved smoothly.
Actually, I living in the country where Directions API are not available.
that’s why I ask to you like this.
If you possible, Could you let me know how to implement marker on map like this?
I would be grateful if you could attach any sample codes.
To move the marker smoothly you have to set it's value using an AnimationController and animate to your final value instead of setting it directly.
You should monetize your channel mann.. b'coz you are giving the GitHub code free... Thanks Man..
I do monetise my channel :) I'm hoping less people will use ad block to support the channel over the long run :)
If you implemented this on a pushed route and pop and pushed the route again, you will encounter:
Flutter : Bad state: Stream has already been listened to
My solution to this is update controller in LocationService:
import 'package:rxdart/rxdart.dart';
final StreamController _locationController = BehaviorSubject();
Then it will work perfectly again!
Yeah, a behavior subject there works. or a broadcast controller as well.
Love how you put location on the vid as well, good vid!
❤️🇿🇦