Flutter Springboot Connection Refused

Juice🌱·2024년 3월 16일

Flutter

목록 보기
4/4

연동에러

Flutter와 SpringBoot 연동하는 프로젝트를 하고 있는데, Flutter에서 Get 하고 싶어서 final url = Uri.parse('http://127.0.0.1:8080/mock/schedule/all/up-coming'); react와 springboot 연동할 때 처럼 localhost나 127.0.0.1로 했더니

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: ClientException with SocketException: Connection refused (OS Error: Connection refused, errno = 111), address = 127.0.0.1, port = 41726, uri=http://127.0.0.1:8080/mock/schedule/all/up-coming

이런 에러가 나오는데

왜?

localhost는 같은 장치(노트북)에서 실행될 때만 가능한것!! 나는 SpringBoot는 맥북에서, 실행 기기는 갤럭시 폰이여서 안되던것!!!

해결책

Flutter의 localhost 대신 노트북의 IP주소를 넣어준다!

IP주소 찾는법

터미널 -> ipconfig getifaddr en0 입력
노트북의 ip주소가 나오는데 그걸 localhost 대신 사용해줍니다!

void fetchSchedules() async {
    final url = Uri.parse('http://192.168.35.51:8080/mock/schedule/all/up-coming');
    final response = await http.get(url);

// resonse.statusCode가 200이면, JSON을 파싱하여 upcomingSchedules와 pastSchedules에 할당
    if (response.statusCode == 200) {
  List<dynamic> schedulesJson = json.decode(response.body);
  var now = DateTime.now();

  var upcoming = schedulesJson.map((json) => SpringScheduleModel.fromJson(json)).where((schedule) => schedule.scheduleDate!.isAfter(now)).toList();
  var past = schedulesJson.map((json) => SpringScheduleModel.fromJson(json)).where((schedule) => schedule.scheduleDate!.isBefore(now)).toList();

  // upcomingSchedules와 pastSchedules에 할당하기 전에 출력
  print("Upcoming Schedules:");
  for (var schedule in upcoming) {
    print("${schedule.title}, Date: ${schedule.scheduleDate}");
  }

  print("Past Schedules:");
  for (var schedule in past) {
    print("${schedule.title}, Date: ${schedule.scheduleDate}");
  }

  upcomingSchedules.assignAll(upcoming);
  pastSchedules.assignAll(past);
} else {
  print("Failed to fetch schedules from Spring Boot");
}
  }

Flutter가 SpringBoot에서 성공적으로 읽었을 때(response의 statuscode가 200일때) print될까요??

성공~~

profile
선한 영향력으로 세상을 변화시키는 새싹개발자

0개의 댓글