【发布时间】:2021-07-07 09:40:09
【问题描述】:
为什么当我尝试在 Marker 中创建 onTap 函数时会显示错误:
无法在初始化程序中访问实例成员“上下文”。尝试 用不同的替换对实例成员的引用 expressiondart(implicit_this_reference_in_initializer)
我想在用户点击标记时显示图像!
代码:
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Completer<GoogleMapController> _controller = Completer();
Iterable markers = [];
Iterable _markers = Iterable.generate(
Locations.list.length,
(index) {
return Marker(
markerId: MarkerId(Locations.list[index]['id']),
position: LatLng(
Locations.list[index]['lat'],
Locations.list[index]['lon'],
),
infoWindow: InfoWindow(
title: Locations.list[index]["title"],
),
onTap: () {
showDialog(context: context
...
);
},
);
},
);
@override
void initState() {
setState(
() {
markers = _markers;
},
);
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Maps Sample App'),
backgroundColor: Colors.green[700],
),
body: GoogleMap(
mapToolbarEnabled: false,
mapType: MapType.normal,
initialCameraPosition: CameraPosition(
target: LatLng(47.0603815, 15.4248164), zoom: 12.8),
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
markers: Set.from(markers),
),
),
);
}
}
位置类:
class Locations {
static List<Map<String, dynamic>> list = [
{"title": "one", "id": "1", "" "lat": 47.0357058, "lon": 15.3988135},
{
"title": "two",
"id": "2",
"image": Image.asset('assets/images/1.Radlader.jpg'),
"lat": 47.038418,
"lon": 15.442134
},
{"title": "three", "id": "3", "lat": 47.061431, "lon": 15.424807},
{"title": "four", "id": "4", "lat": 47.092762, "lon": 15.404409},
{"title": "five", "id": "5", "lat": 47.051198, "lon": 15.438810},
{"title": "six", "id": "6", "lat": 47.081459, "lon": 15.453190},
];
}
谢谢!
【问题讨论】:
标签: flutter google-maps dart google-maps-markers