0

I am trying to fetch data from local api project to my flutter project but it's return this error :-

"SocketException: OS Error: Connection refused, errno = 111, address = 127.0.0.1, port = 47852"

and if i try to using any api from cloud it's working with out any problems

and this is the code:-

   import 'package:drowb_down_list/entities/specialitie.dart';
    import 'package:flutter/material.dart';
    import 'dart:async';
    import 'dart:convert';
    import 'package:http/http.dart' as http;
    
    main(){
      runApp(
          MaterialApp(
            home: HomePage(),
          )
      );
    }
    
    class HomePage extends StatefulWidget {
      @override
      _HomePageState createState() => _HomePageState();
    }
    
    class _HomePageState extends State<HomePage> {
    
      List<Speciality> _specialities = List<Speciality>();
    
      Future<List<Speciality>> fetchSpecialites() async{
        var url = "http://127.0.0.1:8084/api/doctorspecialities";
        var responce = await http.get(Uri.parse(url));
    
        var specialites = List<Speciality>();
        if(responce.statusCode == 200)
        {
          var specialitesJson = json.decode(responce.body);
    
          for(var specialityJson in specialitesJson)
          {
            specialites.add(Speciality.fromjson(specialityJson));
          }
        }
        else
          {
            specialites.add(
            Speciality(
              ID: "aaa",
              Name: "aaa Name",
              Code: "ddss",
              Description: "ssdad",
              EnglishName: "asdasd"
            ));
    
          }
        return specialites;
      }
    
    
      @override
      Widget build(BuildContext context) {
        fetchSpecialites().then((value) {
          _specialities.addAll(value);
        });
        return Scaffold(
          appBar: AppBar(
            title: Text("List view from JSON"),
          ),
          body: ListView.builder(
            itemBuilder: (context, index){
              return Card(
                child: Column(
                  children: [
                   Text(_specialities[index].ID),
                    Text(_specialities[index].Name)
                    /*Text("_specialities[index].ID"),
                    Text("_specialities[index].Name")*/
                  ],
                ),
              );
            },
            itemCount: _specialities.length,
          ),
        );
      }
    }

thank you.

  • you may want to refer this link:- https://api.flutter.dev/flutter/dart-core/Uri/Uri.http.html – Manish Kumar Jul 11 '21 at 16:43
  • Refer this https://stackoverflow.com/questions/47372568/how-to-point-to-localhost8000-with-the-dart-http-package-in-flutter/47379000#47379000 @Abdallh Elmasry – Abhijith Jul 11 '21 at 17:21

0 Answers0