我正在使用一个下拉按钮来更新用户周围餐馆的位置,但是这些位置在ListView中没有更新。这是我到目前为止的内容:
编辑:添加了Init语句
初始化声明
@override
void initState() {
_dropDownMenuItems = buildAndGetDropDownMenuItems(changeDistance);
_mapDropdownFilter = _dropDownMenuItems[0].value;
filterMarker(userLat, userLong, dispensaries, 1);
super.initState();
}
我开设了一个班级来协助这个所谓的地点
class Locations{
final Restaurant dis;
final double dist;
final int index;
Locations({this.dis, this.dist, this.index});
}
然后调用此过滤器以按距离对我的位置进行排序,并给他们与用户之间的距离(以英里为单位):
Future filterMarker(userLat, userLong, restaurants, filterdis) async {
Set<Marker> _tmpMarkers = Set();
final Uint8List markerIcon =
await getBytesFromAsset('assets/smiley.png', 100);
int filterCounter = 0;
List<Locations> _tmpLocation = List();
for (int i = 0; i < restaurant.length; ++i) {
Geolocator()
.distanceBetween(
userLat, userLong, restaurants[i].lat, restaurants[i].long)
.then((calcDist) {
print(calcDist);
if (calcDist / 1000 < filterdis) {
filterCounter++;
final String markerIdVal = 'marker_id_$filterCounter';
final MarkerId markerId = MarkerId(markerIdVal);
var dis = calcDist / 1000;
var distances = dis.toStringAsFixed(2);
// creating a new MARKER
_tmpMarkers.add(Marker(
markerId: markerId,
position: LatLng(restaurants[i].lat, restaurants[i].long),
infoWindow: InfoWindow(
title: restaurants[i].name, snippet: '$distances mi Away'),
icon: BitmapDescriptor.fromBytes(markerIcon)));
_tmpLocation.add(
Locations(index: i, dis: restaurants[i], dist: calcDist / 1000));
}
});
setState(() {
_filteredMarkers = _tmpMarkers;
filteredVenues = _tmpLocation.cast<Locations>();
});
print('Do once');
}
print('There are ${filteredVenues.length} _filteredMarkers');
}
我的下拉按钮的设置如下:
DropdownButton(
style: TextStyle(
fontFamily: 'Roboto-Regular',
color: Colors.black),
elevation: 6,
icon: Icon(
FontAwesomeIcons.angleRight,
color: buddiesGreen,
),
value: _mapDropdownFilter,
items: _dropDownMenuItems,
onChanged: _changeFilterList,
)
填写以下内容:
//Distance Map filter Button
final List changeDistance = ['1', '5 ', '10 ', '20'];
// Set<Marker> _filteredMarkers = Set();
List<Locations> filteredVenues;
var _filteredMarkers;
var filteredRestaurant;
_dropDownMenuItems = buildAndGetDropDownMenuItems(changeDistance);
_mapDropdownFilter = _dropDownMenuItems[0].value;
String _mapDropdownFilter;
List<DropdownMenuItem<String>> _dropDownMenuItems;
List<DropdownMenuItem<String>> buildAndGetDropDownMenuItems(List tempList) {
List<DropdownMenuItem<String>> items = List();
for (String x in tempList) {
items.add(DropdownMenuItem(
value: x,
child: Text(
x,
style: TextStyle(
fontSize: 14,
fontFamily: 'Roboto-Regular',
fontWeight: FontWeight.w700,
color: buddiesPurple),
)));
}
return items;
}
void _changeFilterList(String value) {
//use _filterDropdown for switch statement
setState(() {
_mapDropdownFilter = value;
});
_sortProductsDropDown(_mapDropdownFilter);
print(_mapDropdownFilter);
}
void _sortProductsDropDown(_mapDropdownFilter) {
switch (_mapDropdownFilter) {
case '1 Mile':
print('1 Mile');
filterMarker(userLat, userLong, restaurant, 1);
print(restaurant.length);
break;
case '5 Miles':
filterMarker(userLat, userLong, restaurant, 5);
print(restaurant.length);
break;
case '10 Miles':
filterMarker(userLat, userLong, restaurant, 10);
print(restaurant.length);
break;
case '20 Miles':
filterMarker(userLat, userLong, restaurant, 20);
print(restaurant.length);
break;
}
}
当我点击下拉列表(断开连接是什么)时,它没有更新,为什么当用户选择不同的距离时它没有更新。
答案 0 :(得分:0)
因为您没有在initState()中使用setState()来获取位置列表。因此,即使您正确获取所有内容,位置状态也不会更新。
..
setState(){
_dropDownMenuItems = buildAndGetDropDownMenuItems(changeDistance);
..
}
做这样的事情。