类型的值?''可以将MapType'
在下面附加的代码的第91行中,我会收到这样的错误:
错误:类型为“对象”的值?无法分配给类型“ maptype”的变量。
- “对象”来自“ Dart:core”。
- 'MapType' is from 'package:google_maps_flutter_platform_interface/src/types/ui.dart' ('/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/google_maps_flutter_platform_interface-2.1.5/lib/src /types/ui.dart')。 maptye = value; ^
代码:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:share/share.dart';
class maps extends StatefulWidget {
const maps({Key? key}) : super(key: key);
@override
_mapsState createState() => _mapsState();
}
class _mapsState extends State<maps> {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MapSample(),
);
}
}
class MapSample extends StatefulWidget {
@override
State<MapSample> createState() => MapSampleState();
}
class MapSampleState extends State<MapSample> {
Completer<GoogleMapController> _controller = Completer();
static final CameraPosition _kGooglePlex = CameraPosition(
target: LatLng(37.42796133580664, -122.085749655962),
zoom: 14.4746,
);
MapType mapTye = MapType.normal;
Set<Marker> markerList=Set();
late Marker currentMArker;
@override
void initState() {
// TODO: implement initState
super.initState();
currentMArker=Marker(markerId: MarkerId("1"),position: _kGooglePlex.target);
markerList.clear();
markerList.add(currentMArker);
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(title:Text("Google Maps"),actions: [
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(8)),
color: Colors.greenAccent,
shape: BoxShape.rectangle
),
child: Padding(
padding: const EdgeInsets.only(left:8.0),
child: DropdownButton(
style: TextStyle(color: Colors.black,fontSize: 20,),
value: mapTye,
items: [
DropdownMenuItem(
child: Text("Normal"),
value: MapType.normal,
),
DropdownMenuItem(
child: Text("Hybrid"),
value: MapType.hybrid,
),
DropdownMenuItem(
child: Text("Satellite"),
value: MapType.satellite
),
DropdownMenuItem(
child: Text("Terrain"),
value: MapType.terrain
)
],
onChanged: (value) {
setState(() {
mapTye = value;
});
}),
),
),
],),
body: GoogleMap(
mapType: mapTye,
initialCameraPosition: _kGooglePlex,
markers: markerList,
onCameraIdle: (){
setState(() {
markerList.clear();
markerList.add(currentMArker);
print("Markers $currentMArker");
});
},
onCameraMove: (campposition){
currentMArker=Marker(markerId: MarkerId("1"),position: campposition.target);
},
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
),
floatingActionButton: FloatingActionButton.extended(
onPressed: _goToTheLake,
label: Text('Share Location!'),
icon: Icon(Icons.directions_boat),
),
);
}
Future<void> _goToTheLake() async {
if(currentMArker==null)
{
return;
}
Share.share('https://www.google.com/maps/search/?api=1&query=${currentMArker.position.latitude},${currentMArker.position.longitude}');
}
}
in line 91 of the code which is attached below i am getting an error like this:
Error: A value of type 'Object?' can't be assigned to a variable of type 'MapType'.
- 'Object' is from 'dart:core'.
- 'MapType' is from 'package:google_maps_flutter_platform_interface/src/types/ui.dart' ('/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/google_maps_flutter_platform_interface-2.1.5/lib/src/types/ui.dart').
mapTye = value;
^
CODE :
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:share/share.dart';
class maps extends StatefulWidget {
const maps({Key? key}) : super(key: key);
@override
_mapsState createState() => _mapsState();
}
class _mapsState extends State<maps> {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MapSample(),
);
}
}
class MapSample extends StatefulWidget {
@override
State<MapSample> createState() => MapSampleState();
}
class MapSampleState extends State<MapSample> {
Completer<GoogleMapController> _controller = Completer();
static final CameraPosition _kGooglePlex = CameraPosition(
target: LatLng(37.42796133580664, -122.085749655962),
zoom: 14.4746,
);
MapType mapTye = MapType.normal;
Set<Marker> markerList=Set();
late Marker currentMArker;
@override
void initState() {
// TODO: implement initState
super.initState();
currentMArker=Marker(markerId: MarkerId("1"),position: _kGooglePlex.target);
markerList.clear();
markerList.add(currentMArker);
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(title:Text("Google Maps"),actions: [
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(8)),
color: Colors.greenAccent,
shape: BoxShape.rectangle
),
child: Padding(
padding: const EdgeInsets.only(left:8.0),
child: DropdownButton(
style: TextStyle(color: Colors.black,fontSize: 20,),
value: mapTye,
items: [
DropdownMenuItem(
child: Text("Normal"),
value: MapType.normal,
),
DropdownMenuItem(
child: Text("Hybrid"),
value: MapType.hybrid,
),
DropdownMenuItem(
child: Text("Satellite"),
value: MapType.satellite
),
DropdownMenuItem(
child: Text("Terrain"),
value: MapType.terrain
)
],
onChanged: (value) {
setState(() {
mapTye = value;
});
}),
),
),
],),
body: GoogleMap(
mapType: mapTye,
initialCameraPosition: _kGooglePlex,
markers: markerList,
onCameraIdle: (){
setState(() {
markerList.clear();
markerList.add(currentMArker);
print("Markers $currentMArker");
});
},
onCameraMove: (campposition){
currentMArker=Marker(markerId: MarkerId("1"),position: campposition.target);
},
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
),
floatingActionButton: FloatingActionButton.extended(
onPressed: _goToTheLake,
label: Text('Share Location!'),
icon: Icon(Icons.directions_boat),
),
);
}
Future<void> _goToTheLake() async {
if(currentMArker==null)
{
return;
}
Share.share('https://www.google.com/maps/search/?api=1&query=${currentMArker.position.latitude},${currentMArker.position.longitude}');
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
指定
下拉button
中的类型:
Specify the type in the
DropDownButton
Like so: