用于在地图上使用Json响应的Future Builder



我正在调用API,并利用返回的Json数据在地图上发布标记

我有一个电话在一个单独的应用程序上返回ListView罚款

我试图通过调用相同HTTP的另一个应用程序来实现我的Future构建器,但做了一些修改。

在尝试实现Future Builder后,我现在从Future Builder构建中遇到了错误,我被告知它将修复以前的错误,因为它是强制性的,尽我初学者的最大能力!

我正在拉动stations.place.location.lat&stations.place.location.lngJson在GeoCoordinates中使用以放置来自我的API调用的标记

这是我正在使用的Dart代码,任何指导都将不胜感激。我将排除与此问题无关的代码。

Future Builder(在下面的例子中还有一些粗略的代码(需要进入我的Main.dart

void main() {
SdkContext.init(IsolateOrigin.main);
runApp(MaterialApp(home: MyApp()));
}
class MyApp extends StatefulWidget { 
@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
Future<Stations> stations;


@override
void initState() {
stations = API_Call().fetchStations(); 
super.initState();
}
BuildContext _context;
MapMarkerExample _mapMarkerExample; 


@override
Widget build(BuildContext context) {
_context = context;

return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Example 1'),
),

body:  Container(
child: 
FutureBuilder<Stations>(
future: stations,
builder: (BuildContext context, AsyncSnapshot<Stations> snapshot) {

if (snapshot.hasError)
return Text('Error: ${snapshot.error}');
else

return Stack(
children: [
HereMap(onMapCreated: _onMapCreated),
Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
button('Call', _anchoredMapMarkersButtonClicked),
button('Clear', _clearButtonClicked),
],
),
],
),
],
);
}
);
}
}
)
)
)
);
}
...

api_manager.dart

class MapMarkerExample{


void showAnchoredMapMarkers() {
var stations;

stations = API_Call().fetchStations(); 
for (Station stations in stations) {
GeoCoordinates geoCoordinates = GeoCoordinates (stations.place.location.lat, stations.place.location.lng);

}
GeoCoordinates geoCoordinates = stations.coordinates;
_addPOIMapMarker(geoCoordinates, 1);

}
...

api_call.dart

class API_Call {

Future<Stations> fetchStations() async {
var client = http.Client();
final response = await client.get(
'https://transit.hereapi.com/v8/stations?in=x,-x&return=transport&apiKey=MY_API_KEY');

if (response.statusCode == 200) {
return Stations.fromJson(jsonDecode(response.body)); 
} else {
throw Exception('Failed to load stations');
}
}
}

如果您的服务返回一个列表,您应该将其作为list处理。

List<Station> stations = [];
final responseData = json.decode(response.body);
stations = responseData.map((model) => Station.fromJson(model)).toList();

不要忘记ListView.builder((的itemLength值

最新更新