0

What I'm trying to do is that when I tap a list tile, it generates new marker on its location(based on latitude and longitude) on the map.

but the variables do not update and only show initial value which is 0. If I remove 0 and start hot reload, they print out as null.

class UpdatedStoresListState extends State<UpdatedStoresList> {

  double updatedLat = 0;
  double updatedLon = 0;

  @override
  void initState() {
    super.initState();
  }

it generates new latitude and longitude when I tap ListTile widget.

class UpdatedStoresListState extends State<UpdatedStoresList> {
...

children: <Widget>[
  GestureDetector(
    onTap: () {
      setState(() {
        updatedLat = snapshot.data[index].latitude;
        updatedLon = snapshot.data[index].longitude;
        print('Lat: $updatedLat');
        // 52.902...
        print('Lon: $updatedLon');
        // -129.9031...
      });
    },
    child: ListTile(

I need to use new values for 'Marker' widget. It is in another statefulwidget in different .dart file.

class _MainMapState extends State<MainMap> {
...

Marker(
  width: 40.0,
  height: 40.0,
  point: LatLng(UpdatedStoresListState().updatedLat, UpdatedStoresListState().updatedLon),
  builder: (ctx) => Container(
    child: FlutterLogo(),
  ),
)

I really appreciate any help you can provide

0

2 Answers 2

1

If you are interested in doing it using BLoC follow this: Create a file lat_long_bloc.dart and create a BLoC as bellow in that file.

class LatLongBloc {
   StreamController _latLongStreamController = StreamController<LatLong>();

   Stream get latLongStream => _latLongStreamController.stream;

   dispose(){
     _latLongStreamController.close();
   }

   updateLatLong(LatLng latLong){
     _latLongStreamController.sink.add(latLong);
   }
}

final latLongBloc = LatLongBloc();

And in your GestureDetecture change your code as:

GestureDetector(
    onTap: () {
      updatedLat = snapshot.data[index].latitude;
      updatedLon = snapshot.data[index].longitude;
      latLongBloc.updateLatLong(LatLng(updatedLat , updatedLon ));
    },
    child: ListTile(

And wrap your Marker with StreamBuilder:

StreamBuilder(
        stream: latLongBloc.latLongStream,
        builder: (context, snapshot) {
          LatLng latLog = snapshot.data;
          return Marker(
            width: 40.0,
            height: 40.0,
            point: LatLng(latLog.lat, latLog.long),
            builder: (ctx) => Container(
              child: FlutterLogo(),
            ),
          );
        },
      )

That is it, whenever you will call updateLatLong() of your BLoC your Marker Widget will be updated.

Sign up to request clarification or add additional context in comments.

1 Comment

This is great! your guideline helps me a lot about understanding bloc in my app. thank you
0

point: LatLng(UpdatedStoresListState().updatedLat, UpdatedStoresListState().updatedLon) This is totally wrong, I advice you to start using redux or bloc because you are managing a big and shared app state.

3 Comments

I wanted to avoid advanced structure models as they seem a bit difficult for me to learn, but I didn't notice that I made a big mistake on statefulwidget until you pointed out. Thank you. I'll look into bloc or redux. which one do you recommend?
I recommend redux it's a little bit hard but really effective!
Thank you. I'm worried but also excited to learn redux for my app

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.