Hi folks I'm new with flutter and I building a flutter app with maps and marker, so, I have a class that handles a crud service and receive a specific position, when this class send this position to another class "my maps class" and this class must print a marker in a map. My solution is this, but is very useless because show the previously marker it is not in live. someone can help me pls? thanks.
Class A:
void SaveLatLong() {
_write(); // write a response query in a file request.txt'
Home homeMaps = new Home();
Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => homeMaps)); // go to another class B
}
Class B:
class MapsPage extends StatefulWidget {
static double lat = 0;
static double long = 0;
static double Poilat = 0;
static double Poilong = 0;
static String PoiName = "";
@override
_MapsPageState createState() => _MapsPageState();
}
void getLocation() async {
final service = LocationService();
final locationData = await service.getLocation();
if (locationData != null) {
MapsPage.lat = locationData.latitude!;
MapsPage.long = locationData.longitude!;
}
}
Future<void> _read() async {
String text = "";
final Directory directory = await getApplicationDocumentsDirectory();
final File file = File('${directory.path}/request.txt');
text = await file.readAsString();
MapsPage.Poilat = double.parse(text.split(",").first.replaceAll("[", ""));
MapsPage.Poilong = double.parse(text.split(",")[1]);
MapsPage.PoiName = text.split(",")[5];
}
class _MapsPageState extends State<MapsPage> {
_MapsPageState() {
getLocation();
_read(); // read to file
}
List<Marker> ListOfMarkers = [
new Marker(
width: 45.0,
height: 45.0,
point: new LatLng(MapsPage.Poilat, MapsPage.Poilong),
builder: (context) => new Container(
child: IconButton(
icon: Icon(
Icons.add_location_alt_rounded,
color: Colors.deepPurple,
size: 32,
),
onPressed: () {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text(MapsPage.PoiName),
));
}),
)),
new Marker(
width: 45.0,
height: 45.0,
point: new LatLng(MapsPage.lat, MapsPage.long),
builder: (context) => new Container(
child: IconButton(
icon: Icon(
Icons.person_pin,
color: Colors.blueAccent,
size: 42,
),
onPressed: () {
print('Marker tapped!');
}),
))
];
@override
Widget build(BuildContext context) => Scaffold(
body: Stack(
children: <Widget>[
new FlutterMap(
options: new MapOptions(
minZoom: 10.0, center: new LatLng(34.49, 9.34)),
layers: [
new TileLayerOptions(
urlTemplate:
'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
subdomains: ['a', 'b', 'c']),
new MarkerLayerOptions(markers: ListOfMarkers)
])
],
),
);
DropdownMenuItem<String> buildMenuItem(String item) => DropdownMenuItem(
value: item,
child: Text(
item,
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 14),
),
);
}