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,
      ),
    );
  }
}
14
  • could you show your complete exception/log here? Commented Jan 19, 2021 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. Commented Jan 19, 2021 at 19:28
  • yep! That one. So we have a full context of it. Thanks Commented Jan 19, 2021 at 19:30
  • well... it looks like the port was already open: stackoverflow.com/a/26431210/3743245 Commented Jan 19, 2021 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) Commented Jan 19, 2021 at 19:40

1 Answer 1

0

If your debug launch configuration suddenly won’t launch, try starting the application from the command line and then shutting it down. This helped me fix the issue when I suspected the app didn’t exit properly on the previous run.

Sign up to request clarification or add additional context in comments.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.