如何解决在模拟器上运行的应用程序运行完美,但是在真实设备上运行时,位置标记不可见,并且在运行代码时显示无法加载资产
因为问题是在模拟器中一切正常,...即使在模拟器中成功加载资产,仍然显示堆栈无法加载资产,而且当我在真实设备上运行应用程序后在模拟器上成功测试应用程序后,位置标记不可见,在模拟器中可见
//This is my code
import 'package:flutter_polyline_points/flutter_polyline_points.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:location/location.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'components/map_pin_pill.dart';
import 'Model/pin_pill_info.dart';
const double CAMERA_ZOOM = 16;
const double CAMERA_TILT = 80;
const double CAMERA_BEARING = 30;
const LatLng SOURCE_LOCATION = LatLng(28.357932,77.167889);
const LatLng DEST_LOCATION = LatLng(28.354017,77.1605916);
void main() =>
runApp(MaterialApp(debugShowCheckedModeBanner: false,home: MapPage()));
class MapPage extends StatefulWidget {
@override
State<StatefulWidget> createState() => MapPageState();
}
class MapPageState extends State<MapPage> {
Completer<GoogleMapController> _controller = Completer();
Set<Marker> _markers = Set<Marker>();
// for my drawn routes on the map
Set<Polyline> _polylines = Set<Polyline>();
List<LatLng> polylineCoordinates = [];
PolylinePoints polylinePoints= PolylinePoints();
String googleAPIKey = '';
// for my custom marker pins
BitmapDescriptor sourceIcon;
BitmapDescriptor destinationIcon;
// the user's initial location and current location
// as it moves
LocationData currentLocation;
// a reference to the destination location
LocationData destinationLocation;
// wrapper around the location API
Location location;
double pinPillPosition = -100;
PinInformation currentlySelectedPin = PinInformation(
pinPath: '',avatarPath: '',location: LatLng(0,0),locationName: '',labelColor: Colors.grey);
PinInformation sourcePinInfo;
PinInformation destinationPinInfo;
@override
void initState() {
super.initState();
// create an instance of Location
location = new Location();
polylinePoints = PolylinePoints();
// subscribe to changes in the user's location
// by "listening" to the location's onLocationChanged event
location.onLocationChanged.listen((LocationData cLoc) {
// cLoc contains the lat and long of the
// current user's position in real time,// so we're holding on to it
currentLocation = cLoc;
updatePinOnMap();
});
// set custom marker pins
setSourceAndDestinationIcons();
// set the initial location
setInitialLocation();
}
void setSourceAndDestinationIcons() async {
BitmapDescriptor.fromAssetImage(
ImageConfiguration(devicePixelRatio: 2.0),'assets/driving_pin.png')
.then((onValue) {
sourceIcon = onValue;
});
BitmapDescriptor.fromAssetImage(ImageConfiguration(devicePixelRatio: 2.0),'assets/destination_map_marker.png')
.then((onValue) {
destinationIcon = onValue;
});
}
void setInitialLocation() async {
// set the initial location by pulling the user's
// current location from the location's getLocation()
currentLocation = await location.getLocation();
// hard-coded destination for this example
destinationLocation = LocationData.fromMap({
"latitude": DEST_LOCATION.latitude,"longitude": DEST_LOCATION.longitude
});
}
@override
Widget build(BuildContext context) {
CameraPosition initialCameraPosition = CameraPosition(
zoom: CAMERA_ZOOM,tilt: CAMERA_TILT,bearing: CAMERA_BEARING,target: SOURCE_LOCATION);
if (currentLocation != null) {
initialCameraPosition = CameraPosition(
target: LatLng(currentLocation.latitude,currentLocation.longitude),zoom: CAMERA_ZOOM,bearing: CAMERA_BEARING);
}
return Scaffold(
body: Stack(
children: <Widget>[
GoogleMap(
myLocationEnabled: true,compassEnabled: true,tiltGesturesEnabled: false,markers: _markers,polylines: _polylines,mapType: MapType.normal,initialCameraPosition: initialCameraPosition,onTap: (LatLng loc) {
pinPillPosition = -100;
},onMapCreated: (GoogleMapController controller) {//dikkat
// controller.setMapStyle(Utils.mapStyles);
_controller.complete(controller);
// my map has completed being created;
// i'm ready to show the pins on the map
showPinsOnMap();
}),MapPinPillComponent(
pinPillPosition: pinPillPosition,currentlySelectedPin: currentlySelectedPin),Positioned(
bottom: 90,right: 10,child:
FlatButton(
child: Icon(Icons.pin_drop,color: Colors.white),color: Colors.green,onPressed: updatePinOnMap
)
),],),);
}
void showPinsOnMap() {
// get a LatLng for the source location
// from the LocationData currentLocation object
var pinPosition =
LatLng(currentLocation.latitude,currentLocation.longitude);
// get a LatLng out of the LocationData object
var destPosition =
LatLng(destinationLocation.latitude,destinationLocation.longitude);
sourcePinInfo = PinInformation(
locationName: "Start Location",location: SOURCE_LOCATION,pinPath: "assets/driving_pin.png",avatarPath: "assets/friend1.jpg",labelColor: Colors.blueAccent);
destinationPinInfo = PinInformation(
locationName: "End Location",location: DEST_LOCATION,pinPath: "assets/destination_map_marker.png",avatarPath: "assets/friend2.jpg",labelColor: Colors.purple);
// add the initial source location pin
_markers.add(Marker(
markerId: MarkerId('sourcePin'),position: pinPosition,onTap: () {
setState(() {
currentlySelectedPin = sourcePinInfo;
pinPillPosition = 0;
});
},icon: sourceIcon));
// destination pin
_markers.add(Marker(
markerId: MarkerId('destPin'),position: destPosition,onTap: () {
setState(() {
currentlySelectedPin = destinationPinInfo;
pinPillPosition = 0;
});
},icon: destinationIcon));
// set the route lines on the map from source to destination
// for more info follow this tutorial
setPolylines();
}
void setPolylines() async {
PolylineResult result = await polylinePoints.getRouteBetweenCoordinates(googleAPIKey,PointLatLng(currentLocation.latitude,PointLatLng(destinationLocation.latitude,destinationLocation.longitude));
if(result.points.isNotEmpty) {
print("not empty");
result.points.forEach((PointLatLng point) {
polylineCoordinates.add(LatLng(point.latitude,point.longitude));
});
setState(() {
_polylines.add(Polyline(
width: 3,// set the width of the polylines
polylineId: PolylineId("poly"),color: Color.fromARGB(255,40,122,198),points: polylineCoordinates));
});
}
}
void updatePinOnMap() async {
// create a new CameraPosition instance
// every time the location changes,so the camera
// follows the pin as it moves with an animation
CameraPosition cPosition = CameraPosition(
zoom: CAMERA_ZOOM,target: LatLng(currentLocation.latitude,);
final GoogleMapController controller = await _controller.future;
controller.animateCamera(CameraUpdate.newCameraPosition(cPosition));
// do this inside the setState() so Flutter gets notified
// that a widget update is due
setState(() {
// updated position
var pinPosition =
LatLng(currentLocation.latitude,currentLocation.longitude);
sourcePinInfo.location = pinPosition;
// the trick is to remove the marker (by id)
// and add it again at the updated location
_markers.removeWhere((m) => m.markerId.value == 'sourcePin');
_markers.add(Marker(
markerId: MarkerId('sourcePin'),onTap: () {
setState(() {
currentlySelectedPin = sourcePinInfo;
pinPillPosition = 0;
});
},// updated position
icon: sourceIcon));
});
}
}
//和即将到来的堆栈在下面
Syncing files to device Android SDK built for x86...
D/EGL_emulation( 9741): eglMakeCurrent: 0xa6584ea0: ver 2 0 (tinfo 0x90713ec0)
E/BufferQueueProducer( 9741): [SurfaceTexture-0-9741-1] setAsyncMode: BufferQueue has been abandoned
E/BufferQueueProducer( 9741): [SurfaceTexture-0-9741-1] cancelBuffer: BufferQueue has been abandoned
Restarted application in 3,158ms.
W/DynamiteModule( 9741): Local module descriptor class for providerinstaller not found.
I/DynamiteModule( 9741): Considering local module providerinstaller:0 and remote module providerinstaller:0
W/ProviderInstaller( 9741): Failed to load providerinstaller module: No acceptable module found. Local version is 0 and remote version is 0.
W/ConnectivityManager.CallbackHandler( 9741): callback not found for CALLBACK_CAP_CHANGED message
W/ConnectivityManager.CallbackHandler( 9741): callback not found for CALLBACK_IP_CHANGED message
W/DynamiteModule( 9741): Local module descriptor class for providerinstaller not found.
I/DynamiteModule( 9741): Considering local module providerinstaller:0 and remote module providerinstaller:0
W/ProviderInstaller( 9741): Failed to load providerinstaller module: No acceptable module found. Local version is 0 and remote version is 0.
W/ConnectivityManager.CallbackHandler( 9741): callback not found for CALLBACK_IP_CHANGED message
I/Google Maps Android API( 9741): Google Play services package version: 203019022
D/ ( 9741): HostConnection::get() New Host Connection established 0x872a9500,tid 10180
D/EGL_emulation( 9741): eglCreateContext: 0x8f55fda0: maj 1 min 0 rcv 1
D/EGL_emulation( 9741): eglMakeCurrent: 0xa6584ea0: ver 2 0 (tinfo 0x90713ec0)
I/flutter ( 9741): GOOGLE MAPS URL: https://maps.googleapis.com/maps/api/directions/json?origin=28.3524217%2C77.3368383&destination=28.354017%2C77.16059159999998&mode=driving&avoidHighways=false&avoidFerries=true&avoidTolls=false&key=API_KEY
D/EGL_emulation( 9741): eglMakeCurrent: 0x8f55fda0: ver 1 0 (tinfo 0x8f4360f0)
══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset:
When the exception was thrown,this was the stack:
#0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:223:7)
<asynchronous suspension>
#1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:669:31)
#2 AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:652:14)
#3 ImageProvider.resolveStreamForKey.<anonymous closure> (package:flutter/src/painting/image_provider.dart:505:13)
#4 ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:360:22)
#5 ImageProvider.resolveStreamForKey (package:flutter/src/painting/image_provider.dart:503:80)
#6 ScrollAwareImageProvider.resolveStreamForKey (package:flutter/src/widgets/scroll_aware_image_provider.dart:108:19)
#7 ImageProvider.resolve.<anonymous closure> (package:flutter/src/painting/image_provider.dart:334:9)
#8 ImageProvider._createErrorHandlerAndKey.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:464:26)
(elided 13 frames from dart:async)
Image provider: AssetImage(bundle: null,name: "")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#b540c(),name: "",scale: 1.0)
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ Exception caught by image resource service ════════════════════════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset:
When the exception was thrown,this was the stack:
#0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:223:7)
<asynchronous suspension>
#1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:669:31)
#2 AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:652:14)
#3 ImageProvider.resolveStreamForKey.<anonymous closure> (package:flutter/src/painting/image_provider.dart:505:13)
...
Image provider: AssetImage(bundle: null,scale: 1.0)
════════════════════════════════════════════════════════════════════════════════════════════════════
I/zygote ( 9741): NativeAlloc concurrent copying GC freed 49007(2MB) AllocSpace objects,12(720KB) LOS objects,40% free,8MB/14MB,paused 587us total 174.752ms
I/zygote ( 9741): NativeAlloc concurrent copying GC freed 18774(778KB) AllocSpace objects,0(0B) LOS objects,42% free,paused 1.075ms total 187.684ms
I/zygote ( 9741): NativeAlloc concurrent copying GC freed 4679(318KB) AllocSpace objects,paused 22.482ms total 146.268ms
D/ ( 9741): HostConnection::get() New Host Connection established 0xa3bc5200,tid 9963
D/ ( 9741): HostConnection::get() New Host Connection established 0xa3bc5d40,tid 10271
D/ ( 9741): HostConnection::get() New Host Connection established 0x77dfe380,tid 10294
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。