为什么当我试图在Marker内创建onTap函数时,它会显示错误:
无法在初始值设定项中访问实例成员"context"。尝试将对实例成员的引用替换为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},
];
}
谢谢!
检查代码时,我意识到问题出在_MyAppState 内的标记上