我对flutter世界有点陌生,我正在尝试根据firebase数据库中的一些值在Map中显示一组标记。但到目前为止,标记不会出现,有时在热重新加载后会出现一个标记,但与我在数据库中的标记相去甚远。有人能帮帮我吗?我几乎什么都试过了。这是代码:
class Routes extends StatefulWidget{
static const routeName = '/routes';
@override
State<StatefulWidget> createState() => new _RoutesState();
}
class _RoutesState extends State <Routes> {
GoogleMapController _controller;
Set <Marker> _markers ={};
void _onMapCreated(GoogleMapController controller){
setState(() {
_controller = controller;
retrieveData();
});
}
void iniState(){
super.initState();
setState(() {
retrieveData();
});
}
retrieveData(){
setState(() async {
await Firebase.initializeApp();
final firestoreInstance = FirebaseFirestore.instance;
String place;
double rlat,rlong,speed;
String emailUser = FirebaseAuth.instance.currentUser.email;
firestoreInstance.collection('Cars').where('email', isEqualTo : emailUser).get().then((value){
value.docs.forEach((result) {
print(result.data());
place = result.data()['place'] ;
rlat = double.parse(result.data()['rlat']);
rlong = double.parse(result.data()['rlong']);
_markers.add(
Marker(
markerId: MarkerId('Details'),
position: LatLng(rlat,rlong),
)
);
});
});
});
}
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
title: new Text('Track Route',
style: new TextStyle(color:Colors.white, fontWeight:FontWeight.bold),
),
leading: IconButton(
icon: Icon(Icons.arrow_back,
color: Colors.white),
onPressed: (){
Navigator.of(context).popAndPushNamed(RootPage.routeName);
},
),
),
body: Stack(
children: <Widget>[
GoogleMap(
onMapCreated:_onMapCreated,
initialCameraPosition: CameraPosition(target: LatLng(0,0),zoom:4) ,
markers: _markers,
)
],
) ,
);
}
}
更改此行时会发生什么:
_markers.add(
Marker(
markerId: MarkerId('Details'),
position: LatLng(rlat,rlong),
)
);
对此:
setState(() {
_markers.add(
Marker(
markerId: MarkerId('Details'),
position: LatLng(rlat,rlong),
)
);
});