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

Posted

技术标签:

【中文标题】“未处理的异常:NoSuchMethodError:在 null 上调用了 getter \'iterator\'。”当我在颤振中使用 google_maps_polyline 插件时抛出【英文标题】:"Unhandled Exception: NoSuchMethodError: The getter 'iterator' was called on null." is thrown when I use google_maps_polyline plugin in flutter“未处理的异常:NoSuchMethodError:在 null 上调用了 getter 'iterator'。”当我在颤振中使用 google_maps_polyline 插件时抛出 【发布时间】:2020-05-04 21:55:53 【问题描述】:

由于我一直在使用 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, ***lyn, 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 包和折线包?第一个已经包含折线 因为我试图在两点之间的街道上画线,我发现谷歌地图折线插件可以让我这样做。 但是 google_maps_flutter 已经为您提供了相应的功能 好吧,但是我发现的所有示例都使用了这个插件。我只是想弄清楚这一点。 如果你能试试我峰会的代码会很有用 【参考方案1】:

尝试改变:

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

到:

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

【讨论】:

请说明您的解决方案是如何工作的。【参考方案2】:

Sebastian vargas torres 您需要在您的管理员帐户中激活direction api。

【讨论】:

以上是关于“未处理的异常:NoSuchMethodError:在 null 上调用了 getter 'iterator'。”当我在颤振中使用 google_maps_polyline 插件时抛出的主要内容,如果未能解决你的问题,请参考以下文章

获取“未处理的异常:NoSuchMethodError:方法'map'在null上被调用”

未处理的异常:NoSuchMethodError:方法“findAncestorStateOfType”在 null 上被调用

Flutter,无法提取 api 数据:(未处理的异常:NoSuchMethodError:方法 'map' 在 null 上被调用。)

未处理的异常:NoSuchMethodError:在 null 上调用了方法“[]”(json 解析)

未处理的异常:NoSuchMethodError:在 null 上调用了方法“toRawHandle”

未处理的异常:NoSuchMethodError:在 null 上调用了方法“[]”。尝试调用:[](“is_ordered”)。检查空字段 Firebase [重复]