我正在尝试让我的 Flutter 应用程序显示我的当前位置。虽然调试控制台中显示的代码没有错误,但模拟器只是继续加载而不显示任何结果。我怀疑有某种形式的异步暂停正在发生,但不知道如何解决。
这是我用来获取当前位置的文件 (geolocator_services.dart):
import 'package:geolocator/geolocator.dart';
class GeolocatorService {
Future<Position> getCurrentLocation() async {
return await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high
);
}
}
这是我用来存储我的位置数据的文件 (application_bloc.dart)
import 'package:flutter/material.dart';
import 'package:hawkepedia/services/geolocator_Services.dart';
import 'package:geolocator/geolocator.dart';
class ApplicationBloc with ChangeNotifier {
final geoLocatorService = GeolocatorService();
//Variables
Position? currentLocation;
//fire function when the app starts
ApplicationBloc(){
setCurrentLocation();
}
//gets current location
setCurrentLocation() async {
currentLocation = await geoLocatorService.getCurrentLocation();
notifyListeners();
}
}
这是我用来在我的应用 (maps_mainpage.dart) 中显示地理位置数据的文件。
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:hawkepedia/blocs/application_bloc.dart';
import 'package:provider/provider.dart';
class MainMaps extends StatefulWidget {
const MainMaps({ Key? key }) : super(key: key);
@override
_MainMapsState createState() => _MainMapsState();
}
class _MainMapsState extends State<MainMaps> {
@override
Widget build(BuildContext context) {
final applicationBloc = Provider.of<ApplicationBloc>(context);
return Scaffold(
//if this is the first time loading the app
body: (applicationBloc.currentLocation == null)
? Center(child: CircularProgressIndicator(),)
: ListView(
children: [
TextField(
decoration: InputDecoration(hintText: 'Search Location'),
),
Container(
height: 300.0,
child: GoogleMap(
mapType: MapType.normal,
myLocationButtonEnabled: true,
initialCameraPosition:
CameraPosition(target: LatLng(applicationBloc.currentLocation!.latitude,
applicationBloc.currentLocation!.longitude),
zoom: 14),
),
)
],
),
);
}
}
答案 0 :(得分:0)
将您的 Scaffold 包装到 Consumer 小部件中,确保您还使用相关提供程序小部件包装了 MaterialApp 以便使用它。
late final applicationBloc;
@override
void initState() {
super.initState();
applicationBloc= Provider.of<ApplicationBloc>(context);
applicationBloc.setCurrentLocation();
}
那么,
Consumer(builder:(context,child) {
return Scaffold(
//if this is the first time loading the app
body: (applicationBloc.currentLocation == null)
? Center(child: CircularProgressIndicator(),)
: ListView(
children: [
TextField(
decoration: InputDecoration(hintText: 'Search Location'),
),
Container(
height: 300.0,
child: GoogleMap(
mapType: MapType.normal,
myLocationButtonEnabled: true,
initialCameraPosition:
CameraPosition(target: LatLng(applicationBloc.currentLocation!.latitude,
applicationBloc.currentLocation!.longitude),
zoom: 14),
),
)
],
),
);
}
});
答案 1 :(得分:0)
在调用 Geolocator.getCurrentPosition()
之前,我们需要检查位置服务是否已启用以及我们是否有位置权限。
bool enabled = await Geolocator.isLocationServiceEnabled();
if (!enabled) {
//do something
return;
}
LocationPermission permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
//do something
return;
}
if (permission == LocationPermission.deniedForever) {
// Permissions are denied forever, handle appropriately.
AppSettings.openLocationSettings();
return;
}
try {
_position = await Geolocator.getCurrentPosition();
} catch (e) {
}
答案 2 :(得分:0)
试试这个然后告诉我 maps_mainpage.dart
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:hawkepedia/blocs/application_bloc.dart';
import 'package:provider/provider.dart';
import 'package:untitled/export.dart';
class MainMaps extends StatefulWidget {
const MainMaps({Key? key}) : super(key: key);
@override
_MainMapsState createState() => _MainMapsState();
}
class _MainMapsState extends State<MainMaps> {
late final applicationBloc;
@override
void initState() {
// TODO: implement initState
super.initState();
applicationBloc = Provider.of<ApplicationBloc>(context);
}
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (_) => ApplicationBloc(),
child: Consumer<ApplicationBloc>(builder: (context, value, child) {
return Scaffold(
//if this is the first time loading the app
body: (value.currentLocation == null)
? Center(
child: CircularProgressIndicator(),
)
: ListView(
children: [
TextField(
decoration: InputDecoration(hintText: 'Search Location'),
),
Container(
height: 300.0,
child: GoogleMap(
mapType: MapType.normal,
myLocationButtonEnabled: true,
initialCameraPosition: CameraPosition(
target: LatLng(
applicationBloc.currentLocation!.latitude,
applicationBloc.currentLocation!.longitude),
zoom: 14),
),
)
],
),
);
}),
);
}
}