Flutter 在标记内显示图像';s信息窗口

Flutter 在标记内显示图像';s信息窗口,flutter,google-maps,dart,google-maps-markers,Flutter,Google Maps,Dart,Google Maps Markers,为什么我尝试在标记内创建onTap函数时会显示错误: 无法在初始值设定项中访问实例成员“上下文”。尝试 将对实例成员的引用替换为其他 expressiondart(在初始值设定项中隐含\u此\u引用\u) 我想在用户点击标记时显示图像 代码: void main() => runApp(MyApp()); class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppSta

为什么我尝试在标记内创建onTap函数时会显示错误:

无法在初始值设定项中访问实例成员“上下文”。尝试 将对实例成员的引用替换为其他 expressiondart(在初始值设定项中隐含\u此\u引用\u)

我想在用户点击标记时显示图像

代码:

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},
  ];
}
void main()=>runApp(MyApp());
类MyApp扩展了StatefulWidget{
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类MyAppState扩展了状态{
Completer _controller=Completer();
Iterable标记=[];
Iterable _markers=Iterable.generate(
位置。列表。长度,
(索引){
返回标记(
markerId:markerId(Locations.list[index]['id']),
职位:LatLng(
位置。列表[索引]['lat'],
位置。列表[索引]['lon'],
),
信息窗口:信息窗口(
标题:位置。列表[索引][“标题”],
),
onTap:(){
showDialog(上下文:上下文)
...
);
},
);
},
);
@凌驾
void initState(){
设定状态(
() {
标记=_标记;
},
);
super.initState();
}
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
appBar:appBar(
标题:文本(“地图示例应用程序”),
背景颜色:颜色。绿色[700],
),
正文:谷歌地图(
mapToolbarEnabled:false,
mapType:mapType.normal,
initialCameraPosition:CameraPosition(
目标:拉特林(47.060381515.4248164),变焦:12.8),
onMapCreated:(谷歌地图控制器){
_控制器。完成(控制器);
},
标记:从(标记)开始设置,
),
),
);
}
}
位置类:

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},
  ];
}
类位置{
静态列表=[
{“title”:“one”,“id”:“1”,“lat”:47.0357058,“lon”:15.3988135},
{
"标题":"两个",,
“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中的标记