Hola Soy Dario de argentina. SOy nuevo en esto. Estoy intentando crear un widget que me permita solicitar la ubicacion de un dispositivo en segundo plano. Estoy usando el codigo de ejemplo (https://pub.dev/packages/background_location/example). (aclaracion tiene la dependencia que corresponde)
Mi widget esta escrito de esta manera:
// Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/widgets/index.dart'; // Imports other custom widgets
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom widget code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
import 'package:background_location/background_location.dart';
class Location extends StatefulWidget {
const Location({
Key? key,
this.width,
this.height,
}) : super(key: key);
final double? width;
final double? height;
@override
_LocationState createState() => _LocationState();
}
class _LocationState extends State<Location> {
String latitude = 'waiting...';
String longitude = 'waiting...';
String altitude = 'waiting...';
String accuracy = 'waiting...';
String bearing = 'waiting...';
String speed = 'waiting...';
String time = 'waiting...';
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Background Location Service'),
),
body: Center(
child: ListView(
children: <Widget>[
locationData('Latitude: ' + latitude),
locationData('Longitude: ' + longitude),
locationData('Altitude: ' + altitude),
locationData('Accuracy: ' + accuracy),
locationData('Bearing: ' + bearing),
locationData('Speed: ' + speed),
locationData('Time: ' + time),
ElevatedButton(
onPressed: () async {
//await BackgroundLocation.setAndroidConfiguration(1000);
await BackgroundLocation.startLocationService(
distanceFilter: 20);
BackgroundLocation.getLocationUpdates((location) {
setState(() {
latitude = location.latitude.toString();
longitude = location.longitude.toString();
accuracy = location.accuracy.toString();
altitude = location.altitude.toString();
bearing = location.bearing.toString();
speed = location.speed.toString();
time = DateTime.fromMillisecondsSinceEpoch(
location.time!.toInt())
.toString();
});
print('''\n
Latitude: $latitude
Longitude: $longitude
Altitude: $altitude
Accuracy: $accuracy
Bearing: $bearing
Speed: $speed
Time: $time
''');
});
},
child: Text('Start Location Service')),
ElevatedButton(
onPressed: () {
BackgroundLocation.stopLocationService();
},
child: Text('Stop Location Service')),
ElevatedButton(
onPressed: () {
getCurrentLocation();
},
child: Text('Get Current Location')),
],
),
),
),
);
}
Widget locationData(String data) {
return Text(
data,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
),
textAlign: TextAlign.center,
);
}
void getCurrentLocation() {
BackgroundLocation().getCurrentLocation().then((location) {
print('This is current Location ' + location.toMap().toString());
});
}
@override
void dispose() {
BackgroundLocation.stopLocationService();
super.dispose();
}
}
Pero cuando intento compilar me trae un error desconocido pero no me dice cual es.
Cuando lo excluyo de la compilacion y testeo la app el widget se muestra en la app pero no me actualiza los parametros!.
Alguien me podria ayudar!?
Espero haber explicado bien mi problema!
Desde ya agradecido