它甚至没有显示请求访问用户位置的权限
在此输入图像描述
我正在开发一种类似于Uber的即时送货应用程序,下面的代码是为了在加载时访问用户位置。问题是,代码没有显示任何错误,但加载后不显示谷歌地图,只显示白色背景。
import 'dart:async';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:isntadelivery/Signin.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:geolocator/geolocator.dart';
import 'package:permission_handler/permission_handler.dart' as Thendelo;
import 'package:fluttertoast/fluttertoast.dart';
import 'package:permission_handler/permission_handler.dart';
// import 'package';
class Homepage extends StatefulWidget {
Homepage({Key key}) : super(key: key);
@override
_HomepageState createState() => _HomepageState();
}
class _HomepageState extends State<Homepage> {
GoogleMapController _controller;
GoogleMapController mapController;
Position position;
Widget _child;
Future<void> getpermission() async {
PermissionStatus permission = await PermissionHandler()
.checkPermissionStatus(PermissionGroup.location);
if (permission == PermissionStatus.denied) {
await PermissionHandler()
.requestPermissions([PermissionGroup.locationAlways]);
}
var geolocater = new Geolocator();
GeolocationStatus geolocationStatus =
await geolocater.checkGeolocationPermissionStatus();
switch (geolocationStatus) {
case GeolocationStatus.denied:
showToast('denied');
break;
case GeolocationStatus.disabled:
showToast('disabled');
break;
case GeolocationStatus.restricted:
showToast('restricted');
break;
case GeolocationStatus.unknown:
showToast('unknown');
break;
case GeolocationStatus.granted:
showToast('Access granted');
_getCurrentLocation();
}
}
Set<Marker> _createMarker() {
return <Marker>[
Marker(
markerId: MarkerId('home'),
position: LatLng(position.latitude, position.longitude),
icon: BitmapDescriptor.defaultMarker,
infoWindow: InfoWindow(title: 'Current Location'))
].toSet();
}
void showToast(message) {
Fluttertoast.showToast(
msg: message,
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
// timeInSecFor Ios: 1,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0);
}
void _setStyle(GoogleMapController controller) async {
String value = await DefaultAssetBundle.of(context)
.loadString('assets/map_style.json');
controller.setMapStyle(value);
}
Widget _mapWidget() {
return GoogleMap(
mapType: MapType.normal,
markers: _createMarker(),
initialCameraPosition: CameraPosition(
target: LatLng(position.latitude, position.longitude),
zoom: 12.0,
),
onMapCreated: (GoogleMapController controller) {
_controller = controller;
// _controller.complete(controller);
_setStyle(controller);
},
);
}
//map style variable
void _getCurrentLocation() async {
Position res = await Geolocator().getCurrentPosition();
setState(() {
position = res;
_child = _mapWidget();
});
}
// singoutmethod
signOutGoogle() async {
await googleSignIn.signOut();
print("User Sign Out");
}
// firebase authorisation
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
final GoogleSignIn googleSignIn = GoogleSignIn();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home'),
),
body: _child,
drawer: Drawer(
child: ListView(
// Important: Remove any padding from the ListView.
padding: EdgeInsets.zero,
children: <Widget>[
UserAccountsDrawerHeader(
accountName: new Text('Thabelo Mutshinyani'),
accountEmail: new Text('mutshinyanit@gmail.com'),
currentAccountPicture: new CircleAvatar(),
),
ListTile(
leading: Icon(Icons.person_outline),
title: Text('Update Profile'),
onTap: () {
// Update the state of the app
// ...
// Then close the drawer
Navigator.pop(context);
},
),
ListTile(
leading: Icon(Icons.payment),
title: Text('payment details'),
onTap: () {
// Update the state of the app
// ...
// Then close the drawer
Navigator.pop(context);
},
),
ListTile(
leading: Icon(Icons.settings),
title: Text('Settings'),
onTap: () {
// Update the state of the app
// ...
// Then close the drawer
Navigator.pop(context);
},
),
ListTile(
leading: Icon(Icons.exit_to_app),
title: Text('LogOut'),
onTap: () {
signOutGoogle().whenComplete(() {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) {
return Signin();
},
),
);
});
// Update the state of the app
// ...
// Then close the drawer
Navigator.pop(context);
},
),
],
),
),
);
}
}