🚨 기존 로직의 문제점
기존의 FloatingActionButton을 클릭하여 현재 위치로 이동하는 로직에서 지도 이동이 제대로 작동하지 않는 이유는 다음과 같았다
❌ 문제 1: GoogleMapController가 초기화되지 않은 상태에서 animateCamera 실행될 가능성
final mapController = ref.read(googleMapControllerProvider);
if (mapController != null) {
mapController.animateCamera(CameraUpdate.newLatLng(newPosition));
} else {
print("❌ GoogleMapController가 아직 초기화되지 않았습니다!");
}
📌 문제점:
• ref.read(googleMapControllerProvider)를 호출할 때, GoogleMapController가 아직 null일 수 있음.
• null이면 animateCamera()가 실행되지 않음 → 지도가 이동하지 않음.
• 특히, GoogleMapController는 지도 위젯이 onMapCreated될 때만 설정되므로, 이 전에 animateCamera를 호출하면 문제가 발생함.
❌ 문제 2: 현재 위치 업데이트와 지도 이동이 비동기적으로 실행되면서 타이밍이 맞지 않을 가능성
final newPosition = await ref.read(locationUpdateProvider.future);
ref.read(currentLocationProvider.notifier).state = newPosition;
📌 문제점:
• locationUpdateProvider는 위치 정보를 가져오는 비동기 함수이므로 시간이 걸릴 수 있음.
• currentLocationProvider가 업데이트되더라도, UI가 즉시 반영되지 않을 수 있음.
• UI는 ref.watch(currentLocationProvider)를 사용하여 위치를 감시하지만, 지도는 이를 자동으로 반영하지 않음.
✅ 개선된 방식
기존의 문제를 해결하기 위해 다음과 같은 개선 방법을 적용했어.
🔹 개선 1: Future.delayed(Duration(milliseconds: 500), () { ... }) 추가
📌 목적:
• GoogleMapController가 초기화될 시간을 확보한 후, animateCamera()를 실행.
• FloatingActionButton을 눌렀을 때 위치 업데이트 → 지도 이동을 확실히 순서대로 실행.
Future.delayed(Duration(milliseconds: 500), () {
final mapController = ref.read(googleMapControllerProvider);
if (mapController != null) {
print("✅ 현재 위치 이동: $newPosition");
mapController.animateCamera(CameraUpdate.newLatLng(newPosition));
} else {
print("❌ GoogleMapController가 아직 초기화되지 않음");
}
});
📌 이렇게 개선됨:
✅ 지도 컨트롤러가 null이 아닐 때만 animateCamera를 실행하여 오류 방지
✅ 지도 컨트롤러가 아직 null이면 500ms 후 다시 확인하여 안전하게 이동
🔹 개선 2: currentLocationProvider 변경 후 즉시 지도 이동
📌 목적:
• currentLocationProvider의 상태 변경 후 지도 이동을 보장하기 위해.
final newPosition = await ref.read(locationUpdateProvider.future);
ref.read(currentLocationProvider.notifier).state = newPosition;
Future.delayed(Duration(milliseconds: 500), () {
final mapController = ref.read(googleMapControllerProvider);
if (mapController != null) {
print("✅ 현재 위치 이동: $newPosition");
mapController.animateCamera(CameraUpdate.newLatLng(newPosition));
}
});
📌 이렇게 개선됨:
✅ locationUpdateProvider를 통해 현재 위치 업데이트 후 바로 지도 이동
✅ Future.delayed를 추가하여 지도 컨트롤러 초기화 이후 이동 보장
🚀 최종 개선된 코드 (현재 위치 이동)
FloatingActionButton(
onPressed: () async {
final newPosition = await ref.read(locationUpdateProvider.future);
ref.read(currentLocationProvider.notifier).state = newPosition;
Future.delayed(Duration(milliseconds: 500), () {
final mapController = ref.read(googleMapControllerProvider);
if (mapController != null) {
print("✅ 현재 위치 이동: $newPosition");
mapController.animateCamera(CameraUpdate.newLatLng(newPosition));
} else {
print("❌ GoogleMapController가 아직 초기화되지 않음");
}
});
},
backgroundColor: Colors.grey[200],
shape: CircleBorder(),
child: Icon(Icons.my_location),
),
🎯 최종 개선 결과
문제점기존 코드개선 코드
지도 이동 시 GoogleMapController가 null일 가능성 | animateCamera 호출 시 null이면 지도 이동 불가 | Future.delayed(500ms) 추가하여 컨트롤러가 준비될 때 실행 |
locationUpdateProvider의 상태 변경 후 UI 반영 지연 | currentLocationProvider가 변경되어도 UI에서 바로 반영되지 않을 수 있음 | currentLocationProvider 변경 후 animateCamera로 즉시 지도 이동 |
✅ 현재 위치 버튼 클릭 시 지도 이동이 확실히 작동
✅ 지도 컨트롤러가 null일 경우 안전하게 대기 후 실행
✅ 현재 위치 업데이트 후 지도 반영 보장
'1인 개발 > myapp' 카테고리의 다른 글
[코드개선] MVVM 디자인 패턴의 잘못된 적용 개선하기 - kakao API요청 시 (0) | 2025.02.26 |
---|---|
[회고록] 1인앱 기능개발 회고_001 (0) | 2025.02.24 |