具有Flutter_Map的简单Flutter应用程序无法正常工作

时间:2019-05-02 20:04:33

标签: flutter leaflet

我正在尝试在flutter上运行插件flutter_map。代码如下:

import 'package:flutter_map/flutter_map.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new FlutterMap(
      layers: [
        new TileLayerOptions(urlTemplate:'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png')
      ],
    );
  }
}

但是在设备上运行代码后,出现以下错误:

Performing hot reload...
Syncing files to device SM J510FN...
I/flutter (14355): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (14355): The following NoSuchMethodError was thrown building LayoutBuilder:
I/flutter (14355): The getter 'plugins' was called on null.
I/flutter (14355): Receiver: null
I/flutter (14355): Tried calling: plugins
I/flutter (14355): 
I/flutter (14355): When the exception was thrown, this was the stack:
I/flutter (14355): #0      Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:50:5)
I/flutter (14355): #1      FlutterMapState.build.<anonymous closure>.<anonymous closure> (package:flutter_map/src/map/flutter_map_state.dart:53:62)
I/flutter (14355): #2      MappedListIterable.elementAt (dart:_internal/iterable.dart:414:29)
I/flutter (14355): #3      ListIterable.toList (dart:_internal/iterable.dart:219:19)
I/flutter (14355): #4      FlutterMapState.build.<anonymous closure> (package:flutter_map/src/map/flutter_map_state.dart:54:12)
I/flutter (14355): #5      _LayoutBuilderElement._layout.<anonymous closure> (package:flutter/src/widgets/layout_builder.dart:111:26)
I/flutter (14355): #6      BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2253:19)
I/flutter (14355): #7      _LayoutBuilderElement._layout (package:flutter/src/widgets/layout_builder.dart:107:11)
I/flutter (14355): #8      RenderObject.invokeLayoutCallback.<anonymous closure> (package:flutter/src/rendering/object.dart:1728:58)
I/flutter (14355): #9      PipelineOwner._enableMutationsToDirtySubtrees (package:flutter/src/rendering/object.dart:797:15)
I/flutter (14355): #10     RenderObject.invokeLayoutCallback (package:flutter/src/rendering/object.dart:1728:13)
I/flutter (14355): #11     _RenderLayoutBuilder.performLayout (package:flutter/src/widgets/layout_builder.dart:205:5)
I/flutter (14355): #12     RenderObject.layout (package:flutter/src/rendering/object.dart:1632:7)
I/flutter (14355): #13     RenderView.performLayout (package:flutter/src/rendering/view.dart:151:13)
I/flutter (14355): #14     RenderObject._layoutWithoutResize (package:flutter/src/rendering/object.dart:1507:7)
I/flutter (14355): #15     PipelineOwner.flushLayout (package:flutter/src/rendering/object.dart:766:18)
I/flutter (14355): #16     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding.drawFrame (package:flutter/src/rendering/binding.dart:329:19)
I/flutter (14355): #17     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:701:13)
I/flutter (14355): #18     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:268:5)
I/flutter (14355): #19     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:988:15)
I/flutter (14355): #20     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:928:9)
I/flutter (14355): #21     _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:749:7)
I/flutter (14355): #23     _Timer._runTimers (dart:isolate/runtime/libtimer_impl.dart:382:19)
I/flutter (14355): #24     _Timer._handleMessage (dart:isolate/runtime/libtimer_impl.dart:416:5)
I/flutter (14355): #25     _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)
I/flutter (14355): (elided one frame from package dart:async)
I/flutter (14355): ════════════════════════════════════════════════════════════════════════════════════════════════════
Reloaded 206 of 637 libraries in 7,394ms.

我在做什么错?请注意,我是flutter的新手,这是我在flutter主页提供的教程之后的第一次试用。

2 个答案:

答案 0 :(得分:3)

首先确保pubspec.yaml具有:

dependencies:
flutter:
  sdk: flutter
flutter_map: ^0.8.1

...并且您已经在Android Studio中运行了GET PACKAGES(我认为那是您在此处使用的功能)

然后,使用ESRI地图进行尝试,例如:

Widget build(BuildContext context) {
    return new FlutterMap(
      options: new MapOptions(
        center: new LatLng(40.0, -120.0),
        zoom: 8.0,
      ),
      layers: [
        TileLayerOptions(
          urlTemplate:
              'https://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer/tile/{z}/{y}/{x}',
          subdomains: ['a', 'b', 'c'],
          tileProvider: CachedNetworkTileProvider(),
        ),
      ],
    );
  }

此外,这是与传单一起使用的地图的绝佳资源:

https://leaflet-extras.github.io/leaflet-providers/preview/

答案 1 :(得分:0)

MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: Container(
            child: FlutterMap(
              options: MapOptions(
                plugins: [
                  DragMarkerPlugin(),
                ],
                center: LatLng(23.77441, 90.41153),
                zoom: 16.4,
              ),
              layers: [
                TileLayerOptions(
                  urlTemplate:
                      //'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
                      'https://www.gebco.net/data_and_products/gebco_web_services/north_polar_view_wms/mapserv?',
                  subdomains: [
                    'a',
                    'b',
                    'c',
                  ],
                ),
                PolylineLayerOptions(polylines: _polylines),
                DragMarkerPluginOptions(
                  markers: <DragMarker>[
                    DragMarker(
                      point: LatLng(23.7744, 90.4115),
                      width: 80.0,
                      height: 80.0,
                      offset: Offset(0.0, -8.0),
                      builder: (ctx) => Container(
                        child: Icon(Icons.location_on,
                            size: 50, color: Colors.blueAccent),
                      ),
                      onDragStart: (details, point) =>
                          print("Drag Start point $point"),
                      onDragEnd: (details, point) =>
                          print("Drag End point $point"),
                      // onDragUpdate: (details, point) {
                      //   print("on Drag Update $point");
                      // },
                      // onTap: (point) {
                      //   print("on tap $point");
                      // },
                      // onLongPress: (point) {
                      //   print("on long press $point");
                      // },

                      feedbackBuilder: (ctx) => Container(
                        child: Icon(
                          Icons.edit_location,
                          size: 75,
                          color: Colors.redAccent,
                        ),
                      ),
                      feedbackOffset: Offset(0.0, -18.0),
                      updateMapNearEdge: false,
                      nearEdgeRatio: 2.0,
                      nearEdgeSpeed: 1.0,
                    ),
                    DragMarker(
                      point: LatLng(23.7745, 90.4116),
                      width: 80.0,
                      height: 80.0,
                      builder: (ctx) => Container(
                        child: Icon(
                          Icons.location_on,
                          color: Colors.green,
                          size: 50,
                        ),
                      ),
                      onDragStart: (details, point) =>
                          print("Drag Start point $point"),
                      onDragEnd: (details, point) {
                        print('Finished Drag $details $point');
                      },
                      updateMapNearEdge: false,
                    ),
                  ],
                ),
                // PolylineLayerOptions(
                //   polylines: [
                //     Polyline(
                //       points: points,
                //       strokeWidth: 2.0,
                //       color: Colors.red,
                //     )
                //   ],
                // ),
              ],
              children: [
                Align(
                  alignment: Alignment.bottomRight,
                  child: Column(
                    children: <Widget>[
                      button(_onMapTypeButtonPressed, Icons.map, "btn1"),
                      SizedBox(
                        height: 16.0,
                      ),
                      button(_getCurrentLocation, Icons.location_searching,
                          "btn3"),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );