android 下拉按钮 Flutter 错误异常,值为空

wlwcrazw  于 2023-05-15  发布在  Android
关注(0)|答案(2)|浏览(148)

========小部件库捕获到异常===================================================================在构建MyHomePage(dirty,dependencies:[MediaQuery],状态:_MyHomePageState#a3bdf(代码:跟踪1个自动收报机)):应该只有一个项目具有[DropdownButton]的值:标记{markerId:MarkerId(第一个标记),alpha:

1.0,锚:Offset(0.5,1.0),consumeTapEvents:false,可拖动:假,平坦:false,图标:'BitmapDescriptor'的示例,infoWindow:InfoWindow{title:第一个标记,片段:空,锚:偏移(0.5,0.0)},位置:LatLng(37.42267,-122.0838),旋转:0.0,可见:true,zIndex:0.0,onTap:null,onDragStart:null,onDrag:null,onDragEnd:null}。检测到零个或2个或更多[DropdownMenuItem]具有相同的值'package:flutter/src/material/dropdown. dart':Assert失败:第888行位置15:

'items == null || items.isEmpty || value == null ||
              items.where((DropdownMenuItem<T> item) {
                return item.value == value;
              }).length == 1'

    DropdownButton(
                    value: selectedMarker,
                onChanged: ((Marker? marker) {
                  setState(() {
                    selectedMarker = marker!;
                  });
                  print("selectedmarker value ${selectedMarker}");
                  mapController.moveCamera(CameraUpdate.newLatLng(marker!.position));
                }),
            items: markers.map((marker) {
              return DropdownMenuItem(
                value: marker,
                child: Text(marker.markerId.value),
              );
            }).toList(),
          ),

这是导致错误的下拉按钮小部件。现在我想实现的事情是,在从菜单中选择一个项目后,Map上的标记被选中。那已经完成了。现在,如果我按下方向按钮,然后标记将遵循polylinepoints。当我按下按钮时,出现此错误。方向按钮代码如下:

IconButton(
   onPressed: () async {
         if(selectedMarker.markerId == MarkerId("First Marker")){
           controller = AnimationController(
            duration: const Duration(milliseconds: 1000),
               vsync: this,
               );
                 selectedMarker = markers[0];
       for (int i = 0; i < polylinePoints.length && i + 1 != polylinePoints.length; i = i + 1) {
                                markerAnimation = LatLngTween(
                                    begin: polylinePoints[i],
                                    end: polylinePoints[i + 1])
                                    .animate(controller);
                                markerAnimation.addListener(() {
                                  setState(() {
          Marker marker1 = Marker(markerId: MarkerId("First Marker",
                       ),icon:BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueGreen),
                                    );
                                    print("This is marker1 ${marker1}");
                                    marker1 = marker1.copyWith(
                                      positionParam: markerAnimation.value,
                                    );
                                    ///will be right back
                                    markers[0] = marker1;
                                    // _addFirstMarker(Variables.point);
                                  });
                                });
                                controller.forward();
                                await Future.delayed(
                                    const Duration(milliseconds: 50));
                              }
                            }
                            if(selectedMarker.markerId == MarkerId("Second Marker")){
                              controller = AnimationController(
                                duration: const Duration(milliseconds: 1000),
                                vsync: this,
                              );
                 selectedMarker = markers[1];
     for (int i = 0; i < polylinePoints1.length && i + 1 != polylinePoints1.length; i = i + 1) {
                                markerAnimation = LatLngTween(
                                    begin: polylinePoints1[i],
                                    end: polylinePoints1[i + 1])
                                    .animate(controller);
                                markerAnimation.addListener(() {
                                  setState(() {
          Marker marker1 = Marker(markerId: MarkerId("Second Marker",
            ),icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueGreen),
                                    );
                                    marker1 = marker1.copyWith(
                                      positionParam: markerAnimation.value,
                                    );
                                    markers[1] = marker1;
                                    // _addFirstMarker(Variables.point);
                                  });
                                });
                                controller.forward();
                                await Future.delayed(
                                    const Duration(milliseconds: 50));
                              }
                            }
                            if(selectedMarker.markerId == MarkerId("Third Marker")){
                              controller = AnimationController(
                                duration: const Duration(milliseconds: 1000),
                                vsync: this,
                              ); selectedMarker = markers[2];
     for (int i = 0; i < polylinePoints2.length && i + 1 != polylinePoints2.length; i = i + 1) {
                                markerAnimation = LatLngTween(
                                    begin: polylinePoints2[i],
                                    end: polylinePoints2[i + 1])
                                    .animate(controller);
                                markerAnimation.addListener(() {
                                  setState(() {
          Marker marker1 = Marker(markerId: MarkerId("Third Marker",
           ), icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueGreen),
                                    );
                                    marker1 = marker1.copyWith(
                                      positionParam: markerAnimation.value,
                                    );
                                    markers[2] = marker1;
                                    // _addFirstMarker(Variables.point);
                                  });
                                });
                                controller.forward();
                                await Future.delayed(
                                    const Duration(milliseconds: 50));
                              }
                            }
                          },
                          icon: Icon(Icons.directions))
jtw3ybtb

jtw3ybtb1#

DropDownButtonvalue必须存在于项目的值中。
当你点击第一个按钮时

selectedMarker = markers[0];

但是它下面的一些行会给它分配一个新的标记

markers[0] = marker1;

现在selectedMarker不再存在于markers中,这是不允许的。我相信应该做

selectedMarker = markers[0];

就在你做完之后

markers[0] = marker1;

而不是在那之前其他人也一样。

2wnc66cl

2wnc66cl2#

此错误是由初始值引起的。当下拉列表的初始值在项目列表中不可用时。例如

DropdownButton<String>(
            value: "z",
            onChanged: ((marker) {}),
            items: ["a", "b", "c"].map<DropdownMenuItem<String>>((value) {
              return DropdownMenuItem<String>(
                value: value,
                child: Text(value),
              );
            }).toList(),
          ),

在上面的示例中,初始值“z”在项目列表中不可用,因此下拉按钮引发Assert。下拉按钮需要一个值属性,该属性应在项目列表中可用。初始值分配如下,

var selectedMarker = markers.first;

相关问题