dart 生成器执行过程中引发了以下StackOverflowError

vyswwuz2  于 2023-09-28  发布在  其他
关注(0)|答案(1)|浏览(97)

我想在游戏开始前建立一个玩家大厅。为此,我定义了8个对象'PlayerSlot',其中包含两个重要的属性:'slot_number'和'player'。基本上,在构建WaitingPage(大厅)时,我希望该游戏的创建者出现在插槽1中。
但我有这个问题:
The error
这是我的准则

class WaitingPlayersPage extends StatefulWidget {

  final Player creatorOfLobby;

  WaitingPlayersPage(this.creatorOfLobby);

  @override
  State<WaitingPlayersPage> createState() => WaitingPlayersPageState();
}

class WaitingPlayersPageState extends State<WaitingPlayersPage> {

  double rangeSlider = 1;
  
  PlayerSlot _playerSlot1 = PlayerSlot(1 ,false, null);
  PlayerSlot _playerSlot2 = PlayerSlot(2 ,false, null);
  PlayerSlot _playerSlot3 = PlayerSlot(3 ,false, null);
  PlayerSlot _playerSlot4 = PlayerSlot(4 ,false, null);
  PlayerSlot _playerSlot5 = PlayerSlot(5 ,false, null);
  PlayerSlot _playerSlot6 = PlayerSlot(6 ,false, null);
  PlayerSlot _playerSlot7 = PlayerSlot(7 ,false, null);
  PlayerSlot _playerSlot8 = PlayerSlot(8 ,false, null);

  @override
  void initState() {
    _playerSlot1.fillSlotWithPlayer(widget.creatorOfLobby);
  }
  
  @override
  Widget build(BuildContext context) {
    List<PlayerSlot> multiPlayerSlot = [
      _playerSlot1, _playerSlot2,
      _playerSlot3,_playerSlot4,
      _playerSlot5, _playerSlot6,
      _playerSlot7, _playerSlot8
    ];
    return Scaffold(
      body: Container( // WHOLE SCREEN
        alignment: Alignment.center,
        color: Color.fromARGB(255, 236, 131, 166),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            SizedBox(height: 30,),
            Container( // BOX FOR PLAYERS
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10.0),
                color: Colors.pink,
              ),
              
              width: 340,
              height: 500,
              child: Column(
                children: [
                  const Padding(padding: EdgeInsets.symmetric(vertical: 12.0)),
                  _playerSlot1,
                  const Padding(padding: EdgeInsets.symmetric(vertical: 8.0)),
                  _playerSlot2,
                  const Padding(padding: EdgeInsets.symmetric(vertical: 8.0)),
                  _playerSlot3,
                  const Padding(padding: EdgeInsets.symmetric(vertical: 8.0)),
                  _playerSlot4,
                  const Padding(padding: EdgeInsets.symmetric(vertical: 8.0)),
                  _playerSlot5,
                  const Padding(padding: EdgeInsets.symmetric(vertical: 8.0)),
                  _playerSlot6,
                  const Padding(padding: EdgeInsets.symmetric(vertical: 8.0)),
                  _playerSlot7,
                  const Padding(padding: EdgeInsets.symmetric(vertical: 8.0)),
                  _playerSlot8,
                ],
              ),
            ),
            Padding(padding: EdgeInsets.symmetric(vertical: 8.0)),
            
            Container(
              width: 1000,
              child: Slider(
                label: 'Number of round(s) : $rangeSlider',

                value: rangeSlider,
                onChanged: (double newRangeSlider) {
                  setState(() {
                    rangeSlider = newRangeSlider;
                  });
                },
                min: 1,
                max: 10,
                divisions: 9,
              ),
            ),
            Padding(padding: EdgeInsets.symmetric(vertical: 8.0)),
             Row( 
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                ElevatedButton(
                onPressed: () {},
                child: Container(
                  padding: EdgeInsets.all(16.0),
                  width: 320,
                  alignment: Alignment.center,
                  child: Text('CANCEL', style: TextStyle(color: Color.fromARGB(255, 236, 131, 166), decoration: TextDecoration.none),),
                ),
              
            ),
            Padding(padding: EdgeInsets.symmetric(vertical: 8.0)),
            ElevatedButton(
                onPressed: () {
                  GlobalKey gameGlobalkey = GlobalKey();
                  //initGame(gameGlobalkey, ,rangeSlider);
                },
                child: Container(
                  alignment: Alignment.center,
                  width: 320,
                  padding: EdgeInsets.all(16.0),
                  child: Text('START', style: TextStyle(color: Color.fromARGB(255, 236, 131, 166), decoration: TextDecoration.none),),
                ),
              ),])
          ],
        ),
      ),
    );
  }
}

class PlayerSlot extends StatelessWidget {
  int slot_number;
  bool filled = false; 

  PlayerSlot(this.slot_number, this.filled,Player? p);
  
  Player get p => Player('Player $slot_number');
  set p(Player newPlayer) => p = newPlayer;

  void fillSlotWithPlayer(Player player) {
    filled = true;
    p = player;
  }

  @override
  Widget build(BuildContext context) {
    String writeInSlot = filled == true ? p.name : '';
    return Container( // ONE PLAYER BOX
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10.0),
        color: Colors.pinkAccent,
      ),
        width: 300,
        height: 40,
        child: Row(
          children: [
            Container(
              width: 160,
              height: 30,
              child: Text(writeInSlot, style: TextStyle(color: Color.fromARGB(255, 236, 131, 166), decoration: TextDecoration.none, fontSize: 18, fontWeight: FontWeight.bold))),
            Padding(padding: EdgeInsets.symmetric(horizontal: 40.0)),
            IconButton(
              onPressed: () {},
              iconSize: 24,
              icon: Icon(Icons.cancel),
              color: Color.fromARGB(255, 236, 131, 166),
            ),
          ],
        ),
      
    );
  }
}

类'Player':

class Player {
  String name;

  Player(this.name);

  set playerName(String newName) => name = newName;

  void bindPlayerToCharacter(List<Character> c) {

  }
}
6tdlim6h

6tdlim6h1#

看起来像是递归调用set p。尝试使用一个声明一个类型为player的私有var,并在get pset p中使用它。

class PlayerSlot extends StatelessWidget {
  int slot_number;
  bool filled = false; 
  Player _player;

  PlayerSlot(this.slot_number, this.filled,Player? p);
  
  Player get p => _player;
  set p(Player newPlayer) => _player = newPlayer;

  void fillSlotWithPlayer(Player player) {
    filled = true;
    p = player;
  }

相关问题