Flutter 中出现 SocketException 问题:无法创建服务器套接字

问题描述 投票:0回答:1

在 YouTube 上的 Flutter Zone 制作视频时遇到了通过 Flutter 运行本地主机的问题。 我得到的错误是“SocketException:无法创建服务器套接字(操作系统错误:通常只允许每个套接字地址(协议/网络地址/端口)使用一次。 ,errno = 10048),地址= 127.0.0.1,端口= 9109”。它应该为locahost调用API,但出现此错误。API工作正常,因为我可以从网站调用它。我已经尝试过寻找基于 flutter 的问题的示例,但不确定此页面是否实际上是我需要解决方案的页面。

如有任何帮助,我们将不胜感激。

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);
    });
  }
}

调试控制台

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

获取食物

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,
      ),
    );
  }
}
flutter
1个回答
0
投票

如果您的调试模式突然无法启动,请尝试从命令行启动应用程序,然后将其关闭。当我怀疑应用程序在上次运行时未正确退出时,这帮助我解决了问题。

© www.soinside.com 2019 - 2024. All rights reserved.