标记不会在 Flutter 中显示
Marker won't be displayed in Flutter
我对 flutter 世界有点陌生,我正在尝试根据我的 firebase 数据库中的某些值在我的地图中显示一组标记。但到目前为止,标记不会出现,有时在热重载后会出现标记,但它远不及我在数据库中的标记。有人可以帮助我吗,我几乎尝试了所有方法。
这是代码:
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),
)
);
});
我对 flutter 世界有点陌生,我正在尝试根据我的 firebase 数据库中的某些值在我的地图中显示一组标记。但到目前为止,标记不会出现,有时在热重载后会出现标记,但它远不及我在数据库中的标记。有人可以帮助我吗,我几乎尝试了所有方法。 这是代码:
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),
)
);
});