Marker doesen't reload flutter map

Viewed 16

I building a map in flutter and write a information of marker in a file txt. After read this file I want to print the markers on a map, I had problems with asynchronous functions but now I fixed it. But my marker in a page not reloaded in time, Why? Thank you all. Code:

class MapsPage extends StatefulWidget {
  @override
  _MapsPageState createState() => _MapsPageState();
}

double lat = 0;
double long = 0;
double Poilat = 0;
double Poilong = 0;
String PoiName = "";
List<Marker> ListOfMarkers = List.empty(growable: true);

Future<void> getLocation() async {
  print("read position");
  final service = LocationService();
  final locationData = await service.getLocation();

  if (locationData != null) {
    lat = locationData.latitude!;
    long = locationData.longitude!;
  }
  print("after position");
  await _read();
}

Future<String> _read() async {
  print("****read***");
  String text = "";
  final Directory directory = await getApplicationDocumentsDirectory();
  final File file = await File('${directory.path}/request.txt');
  text = await file.readAsString();
  Poilat = double.parse(text.split(",").first.replaceAll("[", ""));
  Poilong = double.parse(text.split(",")[1]);
  PoiName = text.split(",")[5];
  print("****after read***");
  return PoiName;
}

class _MapsPageState extends State<MapsPage> {
  _MapsPageState() {}
  @override
  void initState() {
    super.initState();
    start();
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        body: Stack(
          children: <Widget>[
            new FlutterMap(
                options: new MapOptions(
                    minZoom: 10.0, center: new LatLng(44.49, 11.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),
        ),
      );

  Future<void> createMarker() async {
    print("****2***");

    ListOfMarkers = [
      new Marker(
          width: 45.0,
          height: 45.0,
          point: new LatLng(Poilat, 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(PoiName),
                              ));
                    }),
              )),
      new Marker(
          width: 45.0,
          height: 45.0,
          point: new LatLng(lat, long),
          builder: (context) => new Container(
                child: IconButton(
                    icon: Icon(
                      Icons.person_pin,
                      color: Colors.blueAccent,
                      size: 42,
                    ),
                    onPressed: () {
                      print('Marker tapped!');
                    }),
              ))
    ];
    print("****3***");
  }

  Future<void> start() async {
    print("****1***");
    await getLocation();
    await createMarker();
  }
}

Output:

I/flutter ( 4479): *1

I/flutter ( 4479): read position

I/flutter ( 4479): after position

I/flutter ( 4479): *read

I/flutter ( 4479): *after read

I/flutter ( 4479): *2

I/flutter ( 4479): *3

0 Answers
Related