类型的值?''可以将MapType'

发布于 2025-01-29 09:18:19 字数 4145 浏览 3 评论 0原文

在下面附加的代码的第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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酸甜透明夹心 2025-02-05 09:18:19

指定下拉button中的类型

DropdownButton<MapType>(
                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;
                  });
                }),

Specify the type in the DropDownButton

Like so:

DropdownButton<MapType>(
                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;
                  });
                }),
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文