简体   繁体   中英

how to resolve connection refuse in flutter

I am trying to retrieve some information along with images from my backend in my flutter app. The backend is with spring boot and I am getting this error Connection refused (OS Error: Connection refused, errno = 111), address = localhost, port = 56766. The backend is hosted locally.

import 'dart:convert';

import 'package:http/http.dart' as http;
import 'package:vacation_tour_app/model/data_model.dart';

class DataServices{
  String baseUrl = 'http://192.168.43.5:8080/';
  Future<List<DataModel>>getInfo() async {
    var apiUrl = 'tourist/getAll';
    http.Response res = await http.get(Uri.parse(baseUrl+apiUrl));

    try{
      if(res.statusCode==200){
        List<dynamic> list = json.decode(res.body);
        return list.map((e) => DataModel.fromJson(e)).toList();
      }else{
        return <DataModel>[];
      }
    }catch(e){
      print(e);
      return <DataModel>[];
    }
  }
}


class DataModel{
   String name;
   String description;
   String image;
   int price;
   int people;
   int stars;
   String location;
   DataModel({
     required this.price,
     required this.name,
     required this.description,
     required this.image,
     required this.location,
     required this.people,
     required this.stars
});
   factory DataModel.fromJson(Map<String,dynamic>json){
     return DataModel(
     price: json["price"],
     name: json["name"],
   description: json["description"],
   image: json["image"],
   location: json["location"],
   people: json["people"],
   stars: json["stars"]);
   }
}

Seems your trouble lies entirely on your connection, have you tried changing the IP address on your code and/or checking if you're using some kind of firewall, proxy, VPN on your computer that may refuse connections? Would be great if you try those requests on a different environment such as Postman using the same IP address on your Flutter code. PS: It could be also something as trivial as changing your http scheme to https .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM