我在将地图控制器传递给我的自定义微件时遇到错误。如何解决此错误消息?
我已经尝试在 initState(({} 中实例化控制器,但它似乎不起作用。
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
Completer<GoogleMapController> mapController;
LocationData currentLocation;
var location = new Location();
currentLocationCheck() async {...
}
@override
void initState() {
// TODO: implement initState
super.initState();
currentLocationCheck();
mapController = Completer();
}
@override
Widget build(BuildContext context) {
return Scaffold(.....
);
}
错误消息将传递到"列表"微件。然后,此列表小部件在基架小部件中使用
List<Widget> customItemList = [
SizedBox(width: 10),
customListItemBox(
photoUrl:
"http://marjsia.com/wp-content/uploads/2013/08/P1100993-1024x683.jpg",
title: "Glorietta 2",
description: "Glorietta 2 J.Co Donuts",
latitude: 14.550664464,
longitude: 121.021833246,
mapController: mapController,
),
];
}
Widget customListItemBox(
{String photoUrl,
String title,
String description,
double latitude,
double longitude,
Completer mapController}) {
return GestureDetector(
onTap: () {
_goToLocation(latitude, longitude, mapController);
},
child: Padding(.....
),
);
}
这是使用控制器的函数
Future<void> _goToLocation(
double latitude, double longitude, Completer mapController) async {
final GoogleMapController controller = await mapController.future;
controller.animateCamera(
CameraUpdate.newCameraPosition(
CameraPosition(
target: LatLng(latitude, longitude),
zoom: 15,
tilt: 50.0,
bearing: 45,
),
),
);
}
你可以在 initState(( 中初始化 customItemList
class SearchResultsState extends State<Map> {
Completer<GoogleMapController> _controller = Completer();
List<Widget> customItemList = [];
@override
void initState() {
super.initState();
//_controller = Completer();
customItemList = [
SizedBox(width: 10),
customListItemBox(
photoUrl:
"http://marjsia.com/wp-content/uploads/2013/08/P1100993-1024x683.jpg",
title: "Glorietta 2",
description: "Glorietta 2 J.Co Donuts",
latitude: 14.550664464,
longitude: 121.021833246,
mapController: _controller,
),
];
}