我正在尝试转换Google Places API调用的JSON结果以适合我的Restaurant对象,但是,对于Flutter来说,这似乎是新问题。我的搜索结果被放入列表中,但是您可以看到我的Restaurant班级希望有一张地图。
这是我的餐厅课程
class Restaurant{
final String id;
final String name;
final GeoPoint location;
final String image;
const Restaurant({
this.id,
this.name,
this.location,
this.image,
});
Restaurant.fromMap(Map<String, dynamic> data, String id)
: this(
id: id,
name: data['name'],
location: data['location'],
image: data['image'],
);
}
我身体的相关部位:
Expanded(
child: new FutureBuilder(
future: _updateRestaurantsNearby(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.connectionState == ConnectionState.none &&
snapshot.hasData == null) {
return Container();
}
if (!snapshot.hasData) return _buildLoadingIndicator();
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return new RestaurantCard(
restaurant: Restaurant.fromMap(
snapshot.data[index], snapshot.data[index].id),
);
最后是我的用于处理JSON数据的类。我正在使用google_maps_webservice程序包:
Future _updateRestaurantsNearby() async {
PlacesSearchResponse response = await places.searchNearbyWithRadius(
new Location(lat, long),
radius,
);
if (response.hasNoResults) {
print("No results");
return null;
}
var nearbyRestaurants;
var results = response.results;
results.forEach((f) {
Restaurant restaurant = Restaurant(
id: f.id,
name: f.name,
location: GeoPoint(f.geometry.location.lat, f.geometry.location.lng),
image: null);
nearbyRestaurants.update(restaurant);
//print(nearbyRestaurants.length);
});
return nearbyRestaurants;
}
我想将上面循环中的数据转换为Map而不是List,因此我可以在ListView构建器中使用它们,只是不确定如何实现。