I have a problem with getting a geolocation on flutter - flutter

I' am trying to get geolocation and then push it on other route, but it returns me smth like:
W/ActivityThread(27303): SCHED: razewp.covid19_20/.MainActivity [81, r=36ms, a=8ms, w=31795ms]
here's code:
its my first file loc_get.dart
import 'package:geolocator/geolocator.dart';
class LocationGetter {
var test;
LocationGetter({
this.test,
});
Future<void> getLoc() async {
final position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
}
}
this is my loc.dart:
import 'package:flutter/material.dart';
import 'package:covid19_20/pages/loc_get.dart';
class Location extends StatefulWidget {
#override
_LocationState createState() => _LocationState();
}
class _LocationState extends State<Location> {
void setupLoc() async {
LocationGetter location = LocationGetter(
test: '',
);
await location.getLoc();
Navigator.pushReplacementNamed(context, '/loading', arguments: {
'test': location.test,
});
}
#override
void initState() {
super.initState();
setupLoc();
}
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Center(
child: Text('Getting location...')
),
)
);
}
}
How can I fix it? Is there any ways? I'm debugging on my MI 8 SE phone

Try to change
Future<void> getLoc() async {
final position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
}
to
Future<void> getLoc() async {
final position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
this.test=position;
}

Related

Flutter GetX dependency Injection

I'm new to GetX flutter state management. I'm using two controllers, one for Login and other for Home data(fetching some restaurants data through API call). I'm having trouble in bindings. I'm using bindings in my app following GetX docs. But I'm unable to use it properly and getting error. Following is the code -:
main.dart
void main() async {
await GetStorage.init('My Storage');
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return GetMaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flunkey Task',
getPages: [
GetPage(
name: '/',
page: () => LandingPage(),
binding: BindingsBuilder(() {
Get.lazyPut<LoginController>(() => LoginController());
})),
GetPage(
name: '/login',
page: () => LoginScreen(),
binding: BindingsBuilder(() {
Get.lazyPut<LoginController>(() => LoginController());
})),
GetPage(
name: '/home',
page: () => HomeScreen(),
binding: BindingsBuilder(() {
Get.lazyPut<HomeController>(() => HomeController());
}),
)
],
initialRoute: '/',
);
}
}
class LandingPage extends StatelessWidget {
LandingPage({Key? key}) : super(key: key);
final _controller = Get.find<LoginController>();
#override
Widget build(BuildContext context) {
SizeConfig().init(context);
return Obx(() =>
_controller.isLoggedIn.value == true ? HomeScreen() : LoginScreen());
}
}
loginController.dart
class LoginController extends GetxController {
final box = GetStorage('My Storage');
var isLoggedIn = false.obs;
final formKey = GlobalKey<FormState>();
final usernameTED = TextEditingController();
final passwordTED = TextEditingController();
#override
void onInit() {
isLoggedIn(loginStatus);
super.onInit();
}
#override
void onClose() {
usernameTED.dispose();
passwordTED.dispose();
super.onClose();
}
String? checkUsername(String username) {
if (username.isEmpty || username.length < 3 || username.length > 11) {
return 'Username must have 3-11 characters';
}
return null;
}
String? checkPassword(String password) {
if (password.isEmpty || password.length < 3 || password.length > 11) {
return 'Password must have 3-11 characters';
}
return null;
}
Future<void> login() async {
if (!formKey.currentState!.validate()) {
return;
}
if ((usernameTED.text.trim() == 'flunkey' &&
passwordTED.text.trim() == 'password123') ||
(usernameTED.text.trim() == 'user' &&
passwordTED.text.trim() == 'password123')) {
formKey.currentState!.save();
await changeLoginStatus(true);
await saveUserName(usernameTED.text);
usernameTED.clear();
passwordTED.clear();
} else {
Get.snackbar('Login Error', 'User does not exists',
backgroundColor: Colors.red[400]);
}
}
void signOut() async {
await changeLoginStatus(false);
}
Future<void> changeLoginStatus(bool status) async {
await box.write('status', status);
isLoggedIn(status);
}
Future<void> saveUserName(String name) async {
await box.write('name', name);
}
bool get loginStatus => box.read('status') ?? false;
String get currentUserName => box.read('name') ?? '';
}
homeController.dart
class HomeController extends GetxController {
final _isLoading = false.obs;
final _restaurantData = <restau.Datum>[].obs;
#override
void onInit() {
getData();
super.onInit();
}
bool get isLoading => _isLoading.value;
List<restau.Datum> get getRestaurants => _restaurantData;
Future<void> getData() async {
try {
_isLoading(true);
var apiData = await RestaurantDataApiCall.getRestaurantData();
_restaurantData.value = apiData!.data.data;
_isLoading(false);
} catch (e, s) {
print(e);
print(s);
}
}
}
Following is the error I'm getting.
I'm using Get.find() on Login Screen and Get.find() on Home screen as following,
Please guide me how to properly use Bindings in GetX.
I don't like to bind the controllers on route.
I create a MainBind.dart and put inside this class all getx controllers.
class MainBinding implements Bindings {
#override
Future<void> dependencies() async{
Get.lazyPut<AppController>(() => AppController(), fenix: true);
}
}
And in my Main.dart :
void main() async{
WidgetsFlutterBinding.ensureInitialized();
MainBinding mainBinding = MainBinding();
await mainBinding.dependencies();
runApp(const MyApp());
}
In this way I'm sure that Controllers are binded.
But you can try use Put insted lazyPut too..
You can use StatefulWidget with state class which will contain your controller.
E.g.
StateClass bla, bla {
late final yourController = Get.put<YourController>();
#override
dispose() {
Get.delete<YourController>();
}
}
That's it!

how to close the application on clicking cancel in local auth and also when maximum tries exceeds in flutter

I'm new in flutter. I wanted to create an application with local biometrics I have used local auth and i need to have help with
close the application on the click of cancel button in local_auth,
close the application when maximum tries are done.
pause the background untill authentication complete
my code is
import 'dart:async';
import 'package:LogInSignIn.dart';
import 'package:flutter/material.dart';
import 'package:cashhub/homescreen.dart';
import 'package:local_auth/local_auth.dart';
import 'package:flutter/services.dart';
void main() {
setupLocator();
runApp(new MaterialApp(
debugShowCheckedModeBanner: false,
home: new SplashScreen(),
routes: <String, WidgetBuilder>{
'/HomeScreen': (BuildContext context) => new LogInSignIn(),
},
));
}
class SplashScreen extends StatefulWidget {
#override
_SplashScreenState createState() => new _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> {
//final LocalAuthenticationService _localAuth = locator<LocalAuthenticationService>();
final LocalAuthentication auth = LocalAuthentication();
bool _canCheckBiometrics;
List<BiometricType> _availableBiometrics;
String _authorized = 'Not Authorized';
bool _isAuthenticating = false;
startTime() async {
var _duration = new Duration(seconds: 4);
return new Timer(_duration, navigationPage);
}
Future<void> _authenticate() async {
bool authenticated = false;
try {
setState(() {
_isAuthenticating = true;
_authorized = 'Authenticating';
});
authenticated = await auth.authenticateWithBiometrics(
localizedReason: 'Scan your fingerprint to authenticate',
useErrorDialogs: true,
stickyAuth: true);
setState(() {
_isAuthenticating = false;
_authorized = 'Authenticating';
});
} on PlatformException catch (e) {
print(e);
}
if (!mounted) return;
final String message = authenticated ? 'Authorized' : 'Not Authorized';
// if( message == "Not Authorized"){
// SystemNavigator.pop();
// }
setState(() {
_authorized = message;
});
}
void navigationPage() {
Navigator.of(context).pushReplacementNamed('/HomeScreen');
}
#override
void initState() {
_authenticate();
//autho();
super.initState();
startTime();
}
#override
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new Image.asset('assets/splashlogo.png',
),
),
);
}
}
anyone please help me with this 3 queries..
you can close the app with cancel click like this
setState(() {
if (isAuthorized) {
_authorizedOrNot = "Authorized";
} else {
_authorizedOrNot = "Not Authorized";
exit(0);
}
});
just so you know exit(0) need to impot dart:io

Getting weather of some other location, even after passing correct latitude and longitude

I am using this weather API which requires latitude and longitude to get weather. I am able to get latitude and longitude of my location(tried printing them and they are correct) but when I enter them in my API link, it shows weather of some other latitude and longitude. I have tried putting lats and longs of my location manually in the link, and it works absolutely fine. What is the issue?
import 'package:flutter/material.dart';
import 'package:clima/services/location.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
double key;
class LoadingScreen extends StatefulWidget {
#override
_LoadingScreenState createState() => _LoadingScreenState();
}
class _LoadingScreenState extends State<LoadingScreen> {
var lati;
var longi;
#override
void initState() {
super.initState();
}
void getData() async {
http.Response r = await http.get('http://api.weatherstack.c'
'om/current?access_key=41eb36e0c5f82e3ddce66ef01af877a1&query=$lati,$longi');
String s = r.body;
var data1 = jsonDecode(s)['location']['lat'];
var data2 = jsonDecode(s)['location']['lon'];
print(data1);
print(data2);
print(lati);
print(longi);
}
void getlocation() async {
location a = location();
await a.getclocation();
lati = a.lat;
longi = a.long;
}
Widget build(BuildContext context) {
getlocation();
getData();
return Scaffold();
}
}
Another class named location
import 'package:geolocator/geolocator.dart';
class location{
double lat;
double long;
Future<void> getclocation() async{
// this function is defined in another class
Position position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
lat=position.latitude;
long=position.longitude;
}
}
Outputs are
41.250
1.300
28.7041
77.1025
You need to wait until your operations actually complete.
For a broader explanation on how Future works, see What is a Future and how do I use it?
If you have an async function, make it return a Future<T>, even if it's a Future<void>. Then make sure you await it if you need it's result. In the UI, you cannot just stop and freeze everything, you need a FutureBuilder to play nice and show a little animation while you wait for the result.
class _LoadingScreenState extends State<LoadingScreen> {
Future<String> getDataFuture;
#override
void initState() {
super.initState();
getDataFuture = getData();
}
Future<String> getData() async {
final position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
final r = await http.get('http://api.weatherstack.com/current?access_key=41eb36e0c5f82e3ddce66ef01af877a1&query=$position.latitude,$position.longitude');
return r.body;
}
Widget build(BuildContext context) {
return FutureBuilder(
future: getDataFuture ,
builder: (context, snapshot) {
if(snapshot.hasData) {
return Text('Received response: ${snapshot.data}');
} else if(snapshot.hasError) {
return Text('Error: ${snapshot.error.toString()}');
} else {
return CircularProgressIndicator();
}
}
}

initialRoute string is changed, but I end up at the same page regardless the initialRoute string

When using shared_preferences on flutter in main.dart in order to change the initialRoute depending on if user have seen the first page or if user is logged in I am getting the boolean which is created throughout the app and added to shared_preferences, every time I start app, I get the initialRoute string correct when debugging, but I still end up getting on the first page, regardless the conditions.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:developer';
import './pages/registration.dart';
import './pages/login_page.dart';
import './pages/confirmation.dart';
import './pages/lang_page.dart';
import './pages/main_page.dart';
import './pages/user_data.dart';
import './provider/provider.dart';
void main() => runApp(CallInfoApp());
class CallInfoApp extends StatefulWidget {
#override
_CallInfoAppState createState() => _CallInfoAppState();
}
class _CallInfoAppState extends State<CallInfoApp> {
SharedPreferences prefs;
void getSPInstance() async {
prefs = await SharedPreferences.getInstance();
}
dynamic langChosen;
dynamic isLoggedIn;
String initialRoute;
void dataGetter() async {
await getSPInstance();
setState(() {
langChosen = prefs.getBool('langChosen');
// print(langChosen);
isLoggedIn = prefs.getBool('isLoggedIn');
});
}
void getRoute() async {
await dataGetter();
debugger();
if (langChosen == true && isLoggedIn != true) {
setState(() {
initialRoute = '/login_page';
});
} else if (isLoggedIn == true) {
initialRoute = '/main_page';
} else {
setState(() {
initialRoute = '/';
});
}
}
#override
void initState() {
super.initState();
debugger();
getRoute();
}
#override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
return ChangeNotifierProvider<AppData>(
create: (context) => AppData(),
child: MaterialApp(
title: 'Call-INFO',
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: initialRoute,
routes: {
'/': (context) => LanguagePage(),
'/registration_page': (context) => RegistrationPage(),
'/login_page': (context) => LoginPage(),
'/confirmation_page': (context) => ConfirmationPage(),
'/user_data_page': (context) => UserDataPage(),
'/main_page': (context) => MainPage(),
},
),
);
}
}
Since SharedPreference.getInstance() is an async function it will need some time until the instance is available. If you want to use it for initial route you have to make your main function async and preload it there before your MaterialApp is build.
SharedPreference prefs; //make global variable, not best practice
void main() async {
prefs = await SharedPreference.getInstance();
runApp(CallInfoApp());
}
And remove getSPInstance() from dataGetter
Also keep in midn that prefs.getBool('langChosen') will return null and not false if no entry is made into shared preference so use
langChosen = prefs.getBool('langChosen')??false;
isLoggedIn = prefs.getBool('isLoggedIn')??false;
While this solution will work it's not really good practice. I would recommend to have the initialRoute fixed to a splash screen and handle forwarding to the right page from there. A simple splash screen could look like that:
class SplashScreen extends StatefulWidget {
#override
_SplashScreenState createState() => _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> {
#override
Widget build(BuildContext context) {
return Scaffold(body: Center(child: CircularProgressIndicator()));
}
#override
void initState() {
initSplash();
super.initState();
}
Future<void> initSplash() async {
final prefs = await SharedPreferences.getInstance();
final langChosen = prefs.getBool("lang_chosen") ?? false;
final isLoggedIn = prefs.getBool("logged_in") ?? false;
if (langChosen == true && isLoggedIn != true) {
Navigator.of(context).pushReplacementNamed('/login_page');
} else if (isLoggedIn == true) {
Navigator.of(context).pushReplacementNamed('/main_page');
} else {
Navigator.of(context).pushReplacementNamed('/');
}
}
}
Use initState to derive the data your logic is based on (i.e. fetching shared pref info). And use await keyword so that program will wait until the data is fetched from SharedPrefs. Adding the following code to class _CallInfoAppState should help
#override
void initState() {
super.initState();
dataGetter();
}
void dataGetter() async {
await getSPInstance();
setState(() {
langChosen = prefs.getBool('langChosen');
isLoggedIn = prefs.getBool('isLoggedIn');
});
}

Flutter, problem using InitState method and problem using setState

I have tried many things to get to call an API and get data, this has been satisfactory but I have not been able to do it from the correct method, when I do it from build it works perfectly but it is not the right place, when I try to do it in initState it simply does not it works, it doesn't even execute the print ();
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
class Post {
final List<dynamic> data;
Post({this.data});
factory Post.fromJson(Map<String, dynamic> json) {
return Post(
data: json['response'],
);
}
}
class LoaderPublicity extends StatefulWidget {
#override
State<StatefulWidget> createState() {
// TODO: implement createState
return _LoaderPublicity();
}
}
class _LoaderPublicity extends State<LoaderPublicity> {
List allPublicity;
#override
void initState() {
super.initState();
getPublicity().then((value) {
print(allPublicity);
});
print(allPublicity);
}
//Obtener todas las publicidades desde el api
Future<void> getPublicity() async {
var response = await http.post(
'http://contablenift.co:3008/consult/getGeneralPublicity',
body: {'actividad': "Turistico", 'location': "No"});
print('????????2');
if (response.statusCode == 200) {
setState(() {
allPublicity = Post.fromJson(json.decode(response.body)).data;
});
}
}
#override
Widget build(BuildContext context) {
getPublicity();
// TODO: implement build
return Container(
child: Column(
children: <Widget>[Text('Aqui va la publicidad')],
),
);
}
}
**
Solved the problem, after going to the entire development team we realized that my phone did not have internet, happy day**
The code below should work, I've added a callback in the initState method.
class LoaderPublicity extends StatefulWidget {
#override
State<StatefulWidget> createState() {
// TODO: implement createState
return _LoaderPublicity();
}
}
class _LoaderPublicity extends State<LoaderPublicity> {
List allPublicity;
#override
void initState() {
super.initState();
getPublicity().then((value) {
print(allPublicity);
print(value);
});
}
//Obtener todas las publicidades desde el api
Future<void> getPublicity() async {
var response = await http.post(
'http://contablenift.co:3008/consult/getGeneralPublicity',
body: {'actividad': "Turistico", 'location': "No"})
print('????????2');
if (response.statusCode == 200) {
setState(() {
allPublicity = Post.fromJson(json.decode(response.body)).data;
});
}
}
#override
Widget build(BuildContext context) {
return Container(
child: Column(
children: <Widget>[Text('Aqui va la publicidad')],
),
);
}
}