“未处理的异常:NoSuchMethodError:在null上调用getter'iterator'。”当我在flutter中使用google_maps_polyline插件时会抛出

问题描述 投票:0回答:1

由于我一直在使用google polyline插件,我的应用程序抛出了此异常,我尝试更改版本,但仍然无法正常工作,有人可以帮我解决这个问题,这里是完整的异常:

    [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The getter 'iterator' was called on null.
E/flutter (23542): Receiver: null
E/flutter (23542): Tried calling: iterator
E/flutter (23542): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
E/flutter (23542): #1      new List.from (dart:core-patch/array_patch.dart:54:19)
E/flutter (23542): #2      new List.of (dart:core/list.dart:142:65)
E/flutter (23542): #3      Polyline.clone (package:google_maps_flutter/src/polyline.dart:168:18)
E/flutter (23542): #4      _keyByPolylineId.<anonymous closure> (package:google_maps_flutter/src/polyline.dart:251:41)
E/flutter (23542): #5      MappedIterator.moveNext (dart:_internal/iterable.dart:395:18)
E/flutter (23542): #6      MapMixin.addEntries (dart:collection/maps.dart:172:23)
E/flutter (23542): #7      new Map.fromEntries (dart:core/map.dart:187:17)
E/flutter (23542): #8      _keyByPolylineId (package:google_maps_flutter/src/polyline.dart:249:3)
E/flutter (23542): #9      new _PolylineUpdates.from (package:google_maps_flutter/src/polyline_updates.dart:24:9)
E/flutter (23542): #10     _GoogleMapState._updatePolylines (package:google_maps_flutter/src/google_map.dart:297:26)
E/flutter (23542): <asynchronous suspension>
E/flutter (23542): #11     _GoogleMapState.didUpdateWidget (package:google_maps_flutter/src/google_map.dart:260:5)
E/flutter (23542): #12     StatefulElement.update (package:flutter/src/widgets/framework.dart:4396:58)
E/flutter (23542): #13     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
E/flutter (23542): #14     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5452:14)
E/flutter (23542): #15     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
E/flutter (23542): #16     RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5161:32)
E/flutter (23542): #17     MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5561:17)
E/flutter (23542): #18     Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15)
E/flutter (23542): #19     _LayoutBuilderElement._layout.<anonymous closure> (package:flutter/src/widgets/layout_builder.dart:115:18)
E/flutter (23542): #20     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2412:19)
E/flutter (23542): #21     _LayoutBuilderElement._layout (package:flutter/src/widgets/layout_builder.dart:95:11)
E/flutter (23542): #22     RenderObject.invokeLayoutCallback.<anonymous closure> (package:flutter/src/rendering/object.dart:1823:58)
E/flutter (23542): #23     PipelineOwner._enableMutationsToDirtySubtrees (package:flutter/src/rendering/object.dart:875:15)
E/flutter (23542): #24     RenderObject.invokeLayoutCallback (package:flutter/src/rendering/object.dart:1823:13)
E/flutter (23542): #25     RenderConstrainedLayoutBuilder.layoutAndBuildChild (package:flutter/src/widgets/layout_builder.dart:173:5)
E/flutter (23542): #26     _RenderLayoutBuilder.performLayout (package:flutter/src/widgets/layout_builder.dart:240:5)
E/flutter (23542): #27     RenderObject.layout (package:flutter/src/rendering/object.dart:1724:7)
E/flutter (23542): #28     MultiChildLayoutDelegate.layoutChild (package:flutter/src/rendering/custom_layout.dart:163:11)
E/flutter (23542): #29     _ScaffoldLayout.performLayout (package:flutter/src/material/scaffold.dart:477:7)
E/flutter (23542): #30     MultiChildLayoutDelegate._callPerformLayout (package:flutter/src/rendering/custom_layout.dart:232:7)
E/flutter (23542): #31     RenderCustomMultiChildLayoutBox.performLayout (package:flutter/src/rendering/custom_layout.dart:391:14)
E/flutter (23542): #32     RenderObject._layoutWithoutResize (package:flutter/src/rendering/object.dart:1584:7)
E/flutter (23542): #33     PipelineOwner.flushLayout (package:flutter/src/rendering/object.dart:844:18)
E/flutter (23542): #34     RendererBinding.drawFrame (package:flutter/src/rendering/binding.dart:344:19)
E/flutter (23542): #35     WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:774:13)
E/flutter (23542): #36     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:283:5)
E/flutter (23542): #37     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1102:15)
E/flutter (23542): #38     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1041:9)
E/flutter (23542): #39     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:957:5)
E/flutter (23542): #40     _rootRun (dart:async/zone.dart:1126:13)
E/flutter (23542): #41     _CustomZone.run (dart:async/zone.dart:1023:19)
E/flutter (23542): #42     _CustomZone.runGuarded (dart:async/zone.dart:925:7)
E/flutter (23542): #43     _invoke (dart:ui/hooks.dart:259:10)
E/flutter (23542): #44     _drawFrame (dart:ui/hooks.dart:217:3)

这是我的代码,我从官方插件网站复制了此示例,如果可以测试它的话,将非常有用:

    import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:google_map_polyline/google_map_polyline.dart';

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  int _polylineCount = 1;
  Map<PolylineId, Polyline> _polylines = <PolylineId, Polyline>{};
  GoogleMapController _controller;

  GoogleMapPolyline _googleMapPolyline =
  new GoogleMapPolyline(apiKey: "YOUR KEY HERE");

  List<List<PatternItem>> patterns = <List<PatternItem>>[
    <PatternItem>[], //line
    <PatternItem>[PatternItem.dash(30.0), PatternItem.gap(20.0)], //dash
    <PatternItem>[PatternItem.dot, PatternItem.gap(10.0)], //dot
    <PatternItem>[
      //dash-dot
      PatternItem.dash(30.0),
      PatternItem.gap(20.0),
      PatternItem.dot,
      PatternItem.gap(20.0)
    ],
  ];

  LatLng _mapInitLocation = LatLng(40.683337, -73.940432);

  LatLng _originLocation = LatLng(40.677939, -73.941755);
  LatLng _destinationLocation = LatLng(40.698432, -73.924038);

  bool _loading = false;

  _onMapCreated(GoogleMapController controller) {
    setState(() {
      _controller = controller;
    });
  }

  _getPolylinesWithLocation() async {
    _setLoadingMenu(true);
    List<LatLng> _coordinates =
    await _googleMapPolyline.getCoordinatesWithLocation(
        origin: _originLocation,
        destination: _destinationLocation,
        mode: RouteMode.driving);

    setState(() {
      _polylines.clear();
    });
    _addPolyline(_coordinates);
    _setLoadingMenu(false);
  }

  _getPolylinesWithAddress() async {
    _setLoadingMenu(true);
    List<LatLng> _coordinates =
    await _googleMapPolyline.getPolylineCoordinatesWithAddress(
        origin: '55 Kingston Ave, Brooklyn, NY 11213, USA',
        destination: '8007 Cypress Ave, Glendale, NY 11385, USA',
        mode: RouteMode.driving);

    setState(() {
      _polylines.clear();
    });
    _addPolyline(_coordinates);
    _setLoadingMenu(false);
  }

  _addPolyline(List<LatLng> _coordinates) {
    PolylineId id = PolylineId("poly$_polylineCount");
    Polyline polyline = Polyline(
        polylineId: id,
        patterns: patterns[0],
        color: Colors.blueAccent,
        points: _coordinates,
        width: 10,
        onTap: () {});

    setState(() {
      _polylines[id] = polyline;
      _polylineCount++;
    });
  }

  _setLoadingMenu(bool _status) {
    setState(() {
      _loading = _status;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      darkTheme: ThemeData(brightness: Brightness.dark),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Map Polyline'),
        ),
        body: Container(
          child: LayoutBuilder(
            builder: (context, cont) {
              return Column(
                children: <Widget>[
                  SizedBox(
                    width: MediaQuery.of(context).size.width,
                    height: MediaQuery.of(context).size.height - 175,
                    child: GoogleMap(
                      onMapCreated: _onMapCreated,
                      polylines: Set<Polyline>.of(_polylines.values),
                      initialCameraPosition: CameraPosition(
                        target: _mapInitLocation,
                        zoom: 15,
                      ),
                    ),
                  ),
                  Expanded(
                    child: Align(
                        alignment: Alignment.center,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: <Widget>[
                            RaisedButton(
                              child: Text('Polylines wtih Location'),
                              onPressed: _getPolylinesWithLocation,
                            ),
                            RaisedButton(
                              child: Text('Polylines wtih Address'),
                              onPressed: _getPolylinesWithAddress,
                            ),
                          ],
                        )),
                  ),
                ],
              );
            },
          ),
        ),
        floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        floatingActionButton: _loading
            ? Container(
          color: Colors.black.withOpacity(0.75),
          child: Center(
            child: Text(
              'Loading...',
              style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
            ),
          ),
        )
            : Container(),
      ),
      debugShowCheckedModeBanner: false,
    );
  }
}
google-maps flutter dart location directions
1个回答
0
投票
尝试更改:

origin: '55 Kingston Ave, Brooklyn, NY 11213, USA', destination: '8007 Cypress Ave, Glendale, NY 11385, USA',

至:

origin: '55 Kingston Ave Brooklyn NY 11213', destination: '8007 Cypress Ave Glendale NY 11385',

© www.soinside.com 2019 - 2024. All rights reserved.