基本上,我从一个json文件中获取标记的数据列表,我不想使用setState
,而是使用一个提供程序。一切工作正常,没有错误,但我的标记在flutter_map中没有得到更新
这是主类
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
MapController? osmController;
Future<void> startParam() async {
osmController = MapController();
MarkerListProvider markerListProvider = MarkerListProvider();
await markerListProvider.loadMarkers();
//await AirportMarkers.loadMarkers(context);
}
@override
void initState() {
super.initState();
startParam();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Column(
children: [
SizedBox(
height: 950,
child: FlutterMap(
mapController: osmController!,
options: MapOptions(
center: LatLng(27.7000, 85.3333),
zoom: 8,
minZoom: 3,
// adaptiveBoundaries: true,
interactiveFlags: //InteractiveFlag.none
InteractiveFlag.pinchZoom | InteractiveFlag.drag,
),
children: [
TileLayer(
tileProvider: CachedTileProvider(),
urlTemplate:
// 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}'),
Consumer<MarkerListProvider>(
builder: (BuildContext context, value, Widget? child) {
return MarkerLayer(markers: value.airportMarkers);
// );
}),
],
),
),
],
),
),
);
}
这是提供者类
class MarkerListProvider extends ChangeNotifier {
List<Marker> airportMarkers = [];
Future<void> loadMarkers() async {
double markerWidth = 20;
double markerHeight = 20;
// Load the JSON file containing the markers
final String jsonString =
await rootBundle.loadString('assets/airport_markers.json');
// Decode the JSON data into a List of Maps
final List<dynamic> jsonData = await json.decode(jsonString);
// Create a List of Markers from the JSON data
List<Marker> apMarker = jsonData.map((data) {
final double latitude = data['point']['latitude'];
final double longitude = data['point']['longitude'];
final String text = data['text'];
return Marker(
width: markerWidth,
height: markerHeight,
point: LatLng(latitude, longitude),
builder: (ctx) => MarkerTap(str: text),
);
}).toList();
// Set the airportMarkers list and return it
airportMarkers = apMarker;
notifyListeners();
}
}
我知道解析没有问题,这是因为当我在主类中使用setState
而不使用provider时,所有的标记填充都很好。
Future<void> loadMarkers() async {
double markerWidth = 20;
double markerHeight = 20;
// Load the JSON file containing the markers
final String jsonString =
await rootBundle.loadString('assets/airport_markers.json');
// Decode the JSON data into a List of Maps
final List<dynamic> jsonData = json.decode(jsonString);
// Create a List of Markers from the JSON data
// List<Marker> apMarker = [];
apMarker = jsonData.map((data) {
final double latitude = data['point']['latitude'];
final double longitude = data['point']['longitude'];
final String text = data['text'];
return Marker(
width: markerWidth,
height: markerHeight,
point: LatLng(latitude, longitude),
builder: (ctx) => MarkerTap(str: text),
);
}).toList();
setState(() {
});
}
这里似乎有什么问题?我的数据很大,包含了世界各地的机场。
2条答案
按热度按时间toiithl61#
您必须在应用上注册提供程序,然后从上下文获取数据。
在主要:
在屏幕上你正在获取数据
还可以考虑使用
FutureBuilder()
作为基于外部数据的小部件:ct3nt3jp2#
奇怪的是,我只需要这样做,没有多大意义,但工作。必须示例化loadMarkers与提供者。