0

Working off a video from Flutter Zone on YouTube adn ran across a problem with running a localhost though Flutter. The error I got was "SocketException: Failed to create server socket (OS Error: Only one usage of each socket address (protocol/network address/port) is normally permitted. , errno = 10048), address = 127.0.0.1, port = 9109". It should be calling an API for the locahost but comes up with this error. The API is working correctly as I can call it from a website. I have tried looking for example for the problem based on flutter but not sure if this page is actually the page I need to have the solution in.

any help is appreciated.

import 'dart:convert';
import '../models/food_model.dart';
import 'package:scoped_model/scoped_model.dart';
import 'package:http/http.dart' as https;

class FoodModel extends Model {
  List<Food> _foods = [];

  List<Food> get foods {
    return List.from(_foods);
  }

  void addFood(Food food) {
    _foods.add(food);
  }

  void fetchFoods() {
    
    https
        .get("https://[IPAdress]/mics_app/api/foods/getFoods.php")
        .then((https.Response response) {
      //print("Fetching data: ${response.body}");
      final List fetchedData = json.decode(response.body);
      final List<Food> fetchedFoodItems = [];
      // print(fetchedData);
      fetchedData.forEach((data) {

        
        Food food = Food(
          id: data["id"],
          category: data["category"],
          imagePath: data["imagePath"],
          name: data["name"],
        );
        fetchedFoodItems.add(food);
      });
      _foods = fetchedFoodItems;
      print(_foods);
    });
  }
}

Debug Console

Launching lib\main.dart on sdk gphone x86 arm in debug mode...
lib\main.dart:1
√ Built build\app\outputs\flutter-apk\app-debug.apk.
Unhandled exception:
SocketException: Failed to create server socket (OS Error: Only one usage of each socket address (protocol/network address/port) is normally permitted.
, errno = 10048), address = 127.0.0.1, port = 9109
#0      serveDevTools (package:devtools_server/src/server.dart:199:5)
<asynchronous suspension>
#1      serveDevToolsWithArgs (package:devtools_server/src/server.dart:72:10)
<asynchronous suspension>

Failed to launch DevTools: TimeoutException after 0:00:10.000000: Future not completed

Connecting to VM Service at ws://127.0.0.1:65493/cACJTymFD2A=/ws

fetchedFoods

import 'package:flutter/material.dart';
import 'package:madeincanadastuff/src/scoped-model/food_model.dart';
import 'package:madeincanadastuff/src/widgets/food_category.dart';
import '../widgets/search_field.dart';
import '../widgets/home_top_info.dart';
import '../widgets/bought_foods.dart';
import '../models/food_model.dart';

class HomePage extends StatefulWidget {
  final FoodModel foodModel;
  HomePage(this.foodModel);
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  // List<Food> _foods = foods;

  @override
  void initState() {
    widget.foodModel.fetchFoods();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        padding: EdgeInsets.only(top: 30, left: 20, right: 20),
        children: <Widget>[
          HomeTopInfo(),
          FoodCategory(),
          SizedBox(
            height: 10.0,
          ),
          SearchField(),
          SizedBox(
            height: 10.0,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Text(
                "Frequently bought stuff",
                style: TextStyle(
                  fontSize: 18.0,
                  fontWeight: FontWeight.bold,
                ),
              ),
              GestureDetector(
                onTap: () {},
                child: Text(
                  "View All",
                  style: TextStyle(
                      fontSize: 18.0,
                      fontWeight: FontWeight.bold,
                      color: Colors.orangeAccent),
                ),
              )
            ],
          ),
          SizedBox(height: 10),
          Column(
            children: widget.foodModel.foods.map(_buildFoodItems).toList(),
          ),
        ],
      ),
    );
  }

  Widget _buildFoodItems(Food food) {
    return Container(
      margin: EdgeInsets.only(bottom: 20.0),
      child: BoughtFoods(
        id: food.id,
        name: food.name,
        imagePath: "assets/images/breakfast.png",
        category: food.category,
      ),
    );
  }
}
nobb666
  • 69
  • 7
  • could you show your complete exception/log here? – Mariano Zorrilla Jan 19 '21 at 19:23
  • @MarianoZorrilla I am assuming you mean whats in the Debug Console. Sorry new to Flutter. Its been added to the above question. – nobb666 Jan 19 '21 at 19:28
  • yep! That one. So we have a full context of it. Thanks – Mariano Zorrilla Jan 19 '21 at 19:30
  • well... it looks like the port was already open: https://stackoverflow.com/a/26431210/3743245 – Mariano Zorrilla Jan 19 '21 at 19:32
  • @MarianoZorrilla I got that from the error but I am having trouble with fixing it. The link in the above comment was a little (a lot) over my head) – nobb666 Jan 19 '21 at 19:40
  • where is fetchFoods being used? Could you show your Stateless or Stateful widget using that method? – Mariano Zorrilla Jan 19 '21 at 21:01
  • @MarianoZorrilla just added it to the bottom of the question. Thanks for you time. – nobb666 Jan 19 '21 at 21:11
  • and by any reason HomePage is being redraw? (like having a bottom navigation bar and moving between screens). Other thing I'm noticing, you're talking about a socket which should be used with a Stream (to keep it open) but you're only listening once with a Future and that's it (like a regular GET REST API), you also need to make sure when to dispose your stream and socket when necessary – Mariano Zorrilla Jan 19 '21 at 22:15
  • if that endpoint is not actually a socket, then your logic is not the correct approach. If that data actually changes and update, then a socket needs to be implemented with a stream to "listen" those changes. – Mariano Zorrilla Jan 19 '21 at 22:17
  • @MarianoZorrilla the data for the API does not change once called into the app – nobb666 Jan 19 '21 at 22:19
  • then why do you need a socket for that? sockets are only meant to be used with chats, real time updates/changes, etc – Mariano Zorrilla Jan 19 '21 at 22:45
  • @MarianoZorrilla Let me clarify what the API is, is called 4 fields within a MySql database. Once it has the info it passes it back to the app and then the app should display the info. – nobb666 Jan 19 '21 at 22:49
  • yes... just like any other REST API call. You don't need a socket for that, is a REST API response, you send a GET request, your PHP file should check if any AUTH is needed, then check the SQL database and respond with a echo with the JSON result. – Mariano Zorrilla Jan 20 '21 at 17:47

0 Answers0