如何解决我无法在华为驱动程序中使用 GPS
我有时会出现一个问题,当启用 GPS 时,在运行应用程序之前“没有任何反应”下一行无法执行 示例:
_loadPosition() async {
print('Are u here'); // it shows
Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.bestForNavigation)
.then((Position _getPosition) {
setState(() {
_myPosition = LatLng(_getPosition.latitude,_getPosition.longitude);
CameraPosition cameraPosition =
new CameraPosition(target: _myPosition,zoom: 16);
_googleMapController
.animateCamera(CameraUpdate.newCameraPosition(cameraPosition));
});
}).catchError((e) {
print(e);
});
print('Any thing'); // it's not show
}
手机是华为y9
文件 pubspec.yaml
name: test
description: A new Flutter project.
# The following line prevents the package from being accidentally published to
# pub.dev using `pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots,like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number,respectively.
# In Android,build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS,build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1
environment:
sdk: ">=2.12.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
flutter_localizations: # Add this line
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
firebase_core: ^1.2.0
firebase_auth: ^1.2.0
# firebase_storage: ^8.1.0
firebase_database: ^7.0.0
libphonenumber: ^2.0.0
google_maps_flutter: ^2.0.5
geolocator: ^7.0.3
http: ^0.13.3
provider: ^5.0.0
flutter_typeahead: ^3.1.3
flutter_polyline_points: ^1.0.0
flutter_inner_drawer: ^1.0.0+1
lottie: ^1.0.1
flutter_geofire: ^2.0.0
google_fonts: ^2.1.0
easy_localization: ^3.0.0
flutter_svg: ^0.22.0
connectivity: ^3.0.6
flutter_phone_direct_caller: ^2.0.0
dev_dependencies:
flutter_test:
sdk: flutter
# For information on the generic Dart part of this file,see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application,so that you can use the icons in
# the material Icons class.
uses-material-design: true
assets:
- assets/translations/
# To add assets to your application,add an assets section,like this:
- assets/images/
- assets/json_assets/
# - images/a_dot_ham.jpeg
# An image asset can refer to one or more resolution-specific "variants",see
# https://flutter.dev/assets-and-images/#resolution-aware.
# For details regarding adding assets from package dependencies,see
# https://flutter.dev/assets-and-images/#from-packages
# To add custom fonts to your application,add a fonts section here,# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name,and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts from package dependencies,# see https://flutter.dev/custom-fonts/#from-packages
dart 文件
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_phone_direct_caller/flutter_phone_direct_caller.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart' as Marker;
import 'package:lottie/lottie.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:provider/provider.dart';
import 'package:transfer/Models/Trip.dart';
import 'package:transfer/Widgets/Button.dart';
import 'package:transfer/Widgets/NoInternet.dart';
import 'package:transfer/providers/ConnectivityProviders.dart';
import 'package:transfer/providers/FireBaseDB.dart';
class MainScreen extends StatefulWidget {
static const String screenID = "MainScreen";
@override
_MainScreenState createState() => _MainScreenState();
}
class _MainScreenState extends State<MainScreen> with TickerProviderStateMixin {
Completer<GoogleMapController> _controller = Completer();
late AnimationController _controllerAnimation;
static final CameraPosition _cameraPosition =
CameraPosition(target: LatLng(30.3697373,30.521441),zoom: 14.47);
List<LatLng> pLine = [];
Set<Polyline> polylineSet = {};
late GoogleMapController _googleMapController;
MapType _currentMapType = MapType.normal;
double bottomPosition = 0;
bool _findPage = false;
Map<MarkerId,Marker.Marker> _markersAll = <MarkerId,Marker.Marker>{};
late LatLng _cameraMovePosition;
late LatLng _myPosition = LatLng(30.3697373,30.521441);
late FireBaseDBProvider firebasedb;
// lavel 2
late String dPhone = "",dTime = "";
bool _dComeing = false;
@override
initState() {
super.initState();
getLocationUpdates();
_controllerAnimation = AnimationController(vsync: this);
Provider.of<ConnectivityProviders>(context,listen: false)
.startMonitoring();
firebasedb = Provider.of<FireBaseDBProvider>(context,listen: false);
firebasedb.setMethods(
onApproved: _onApproved,onInTrip: _onInTrip,// onStarted: _onInTrip
onTripDone: _onTripDone
);
}
@override
Widget build(BuildContext context) {
double mapWidth = MediaQuery.of(context).size.width;
double mapHeight = MediaQuery.of(context).size.height;
// return pageUI(mapHeight,mapWidth);
return Consumer<ConnectivityProviders>(
builder: (context,model,child) {
return pageUI(mapHeight,mapWidth,model);
},);
}
Widget pageUI(mapHeight,ConnectivityProviders model) {
return Scaffold(
appBar: AppBar(
title: Text(tr('title')),),backgroundColor: Colors.black54,body: Stack(
children: [
GoogleMap(
polylines: polylineSet,mapType: _currentMapType,padding: EdgeInsets.only(bottom: bottomPosition),myLocationButtonEnabled: true,initialCameraPosition: _cameraPosition,myLocationEnabled: true,onCameraMoveStarted: () {
setState(() {
_controllerAnimation.forward(from: 0.0);
});
},onCameraIdle: () {
setState(() {
_controllerAnimation.forward(from: 0.65);
});
},tiltGesturesEnabled: true,zoomGesturesEnabled: true,zoomControlsEnabled: false,markers: Set<Marker.Marker>.of(_markersAll.values),onCameraMove: (CameraPosition position) {
setState(() {
_cameraMovePosition = position.target;
});
},onMapCreated: (GoogleMapController controller) async {
_controller.complete(controller);
_googleMapController = controller;
// _loadPosition();
_cameraMovePosition =
LatLng(_myPosition.latitude,_myPosition.longitude);
setState(() {
_cameraMovePosition =
LatLng(_myPosition.latitude,_myPosition.longitude);
bottomPosition = 70;
});
},//Marker
Visibility(
visible: true,child: Positioned(
top: (mapHeight - 128 - 240) / 2,right: (mapWidth - 128) / 2,child: IgnorePointer(
ignoring: true,child: Lottie.asset(
"assets/json_assets/expanding_marker.json",width: 128,height: 160,repeat: false,controller: _controllerAnimation,onLoaded: (composition) {
_controllerAnimation.duration = composition.duration;
_controllerAnimation.forward(from: 0.50);
_controllerAnimation.stop(canceled: true);
}),)),//Button GPS AND MAP
Visibility(
visible: true,child: Positioned(
bottom: 110,right: 30,child: Column(
children: <Widget>[
button(_onMapTypeButtonPressed,Icons.map),SizedBox(
height: 16.0,button(_loadPosition,Icons.my_location),],//Button Start Trip
Visibility(
visible: true,child: Positioned(
bottom: 40,left: 30,child: Column(
children: <Widget>[
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.purple,shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(22.0),onPressed: _goConfTrip,child: Container(
width: 150,height: 55,alignment: Alignment.center,child: const Text(
'go',style:
const TextStyle(fontSize: 24,color: Colors.black),)
],/*
lavel 1
find driver
*/
Visibility(
visible: _findPage,child: Opacity(
opacity: .5,child: Container(
height: mapHeight,width: mapWidth,decoration: BoxDecoration(
color: Color(0xFFFAB74E),borderRadius: new BorderRadius.circular(10.0),boxShadow: [
BoxShadow(
color: Colors.grey,blurRadius: 8.0,spreadRadius: 0.8)
]),child: Positioned(
top: 1,bottom: 1,right: 1,left: 1,child: Lottie.asset(
"assets/json_assets/finding.json",repeat: true,/*
lavel 2
driver approv
*/
Visibility(
visible: _dComeing,decoration: BoxDecoration(
color: Color(0x5F000000),child: Padding(
padding: const EdgeInsets.all(20.0),child: Container(
margin: EdgeInsets.symmetric(vertical: 130.0),decoration: BoxDecoration(
color: Color(0xFFFAB74E),borderRadius: new BorderRadius.circular(20.0),boxShadow: [
BoxShadow(
color: Colors.grey,spreadRadius: 0.8)
]),child: Padding(
padding: const EdgeInsets.all(8.0),child: Column(
children: [
Lottie.asset("assets/json_assets/car_coming.json",height: 20,width: 60),Text('The car is on the way to you'),SizedBox(
height: 5,Text('Expected arrival time : ${dTime}'),Row(
crossAxisAlignment: CrossAxisAlignment.center,mainAxisAlignment: MainAxisAlignment.center,children: [
Text('Vehicle driver number : '),ElevatedButton(
onPressed: () async {
await FlutterPhoneDirectCaller.callNumber(
dPhone);
},child: Text(dPhone),//No internet
Visibility(
visible: (model.isOnline != null) ? !model.isOnline : false,child: NoInternet(),)
],);
}
void getLocationUpdates(){
Geolocator.getPositionStream(
desiredAccuracy: LocationAccuracy.bestForNavigation,distanceFilter: 0).listen((Position position){
print('latitude');
print(position.latitude);
_myPosition = LatLng(position.latitude,position.longitude);
CameraPosition cameraPosition =
new CameraPosition(target: _myPosition,zoom: 16);
_googleMapController
.animateCamera(CameraUpdate.newCameraPosition(cameraPosition));
});
}
_loadPosition() async {
print('Are u here');
Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.bestForNavigation)
.then((Position _getPosition) {
setState(() {
_myPosition = LatLng(_getPosition.latitude,zoom: 16);
_googleMapController
.animateCamera(CameraUpdate.newCameraPosition(cameraPosition));
});
}).catchError((e) {
print(e);
});
print('Any thing');
}
_onMapTypeButtonPressed() {
setState(() {
_currentMapType = _currentMapType == MapType.normal
? MapType.satellite
: MapType.normal;
});
}
_goConfTrip() {
return showDialog<void>(
context: context,barrierDismissible: false,// user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: const Text('AlertDialog Title'),content: SingleChildScrollView(
child: ListBody(
children: const <Widget>[
Text('This is a demo alert dialog.'),Text('Would you like to approve of this message?'),actions: <Widget>[
TextButton(
child: const Text('Approve'),onPressed: _finddrivers,TextButton(
child: const Text('Cancel'),onPressed: () {
Navigator.of(context).pop();
},);
},);
}
_finddrivers() {
Navigator.of(context).pop();
setState(() {
_findPage = true;
});
firebasedb.startRequest(_cameraMovePosition.latitude,_cameraMovePosition.longitude);
}
_onApproved(Trip _trip) {
setState(() {
_dComeing = true;
dPhone = _trip.driverID;
dTime = _trip.time;
});
}
void _onInTrip(Trip p1) {}
void _onTripDone(Trip p1) {
}
}
记录问题 https://drive.google.com/file/d/1LKiC4EbRYAsk-7BEF9FInXW0V_SWCtlS/view?usp=sharing 另外,为什么在视频中多次显示消息?喜欢循环。
我也试过使用 location 也不起作用
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。