質問編集履歴

2

dart.padの実行結果を追加しました

2024/01/31 15:45

投稿

ry0559
ry0559

スコア24

test CHANGED
File without changes
test CHANGED
@@ -5,6 +5,11 @@
5
5
  Listのデータをwhereで検索し特定の値だけ更新したいが
6
6
  List内の全てのデータが更新されてしまう問題を解決したいです
7
7
 
8
+ dart.padで実行できます
9
+ holeNo:1のplayerNo:1のスコアだけ更新したいのですが
10
+ playerNo:1のスコアがすべて更新されてしまいます。
11
+ 解決方法はありますでしょうか?
12
+ よろしくお願いします。
8
13
 
9
14
 
10
15
  ### 該当のソースコード
@@ -84,11 +89,27 @@
84
89
  - [ ] その他
85
90
 
86
91
  ##### 上記の詳細・結果
87
- dart.padで実行できます
92
+ 実行結果
88
- holeNo:1playerNo:1のスコアだけ更新したのですが
93
+ holeNo:1, playerNo:1, name:ち, score:1
94
+ holeNo:1, playerNo:2, name:に, score:0
95
+ holeNo:1, playerNo:3, name:さん, score:0
96
+ holeNo:1, playerNo:4, name:し, score:0
89
- playerNo:1のスコアがすべて更新されてしまます。
97
+ holeNo:2, playerNo:1, name:ち, score:1
98
+ holeNo:2, playerNo:2, name:に, score:0
99
+ holeNo:2, playerNo:3, name:さん, score:0
90
- 解決方法はありますでょうか?
100
+ holeNo:2, playerNo:4, name:, score:0
101
+ holeNo:3, playerNo:1, name:いち, score:1
102
+ holeNo:3, playerNo:2, name:に, score:0
103
+ holeNo:3, playerNo:3, name:さん, score:0
91
- よろくお願いします。
104
+ holeNo:3, playerNo:4, name:, score:0
105
+ holeNo:4, playerNo:1, name:いち, score:1
106
+ holeNo:4, playerNo:2, name:に, score:0
107
+ holeNo:4, playerNo:3, name:さん, score:0
108
+ holeNo:4, playerNo:4, name:し, score:0
109
+ holeNo:5, playerNo:1, name:いち, score:1
110
+ holeNo:5, playerNo:2, name:に, score:0
111
+ holeNo:5, playerNo:3, name:さん, score:0
112
+ holeNo:5, playerNo:4, name:し, score:0
92
113
 
93
114
 
94
115
 

1

dart.padで実行できるコードを準備しました。

2024/01/31 15:42

投稿

ry0559
ry0559

スコア24

test CHANGED
File without changes
test CHANGED
@@ -10,14 +10,51 @@
10
10
  ### 該当のソースコード
11
11
 
12
12
  ```dart
13
+ void main() {
14
+ var scoreEntity = ScoreEntity();
15
+ List<PlayerDetail> playerList = [];
16
+ playerList.add(PlayerDetail(1,'いち',0,0,0));
17
+ playerList.add(PlayerDetail(2,'に',0,0,0));
18
+ playerList.add(PlayerDetail(3,'さん',0,0,0));
19
+ playerList.add(PlayerDetail(4,'し',0,0,0));
20
+
21
+
22
+ scoreEntity.addDetail(Detail(1,0,0,playerList));
23
+ scoreEntity.addDetail(Detail(2,0,0,playerList));
24
+ scoreEntity.addDetail(Detail(3,0,0,playerList));
25
+ scoreEntity.addDetail(Detail(4,0,0,playerList));
26
+ scoreEntity.addDetail(Detail(5,0,0,playerList));
27
+
28
+
29
+ var detail = scoreEntity.detailList[0];
30
+ detail.playerList.firstWhere(
31
+ (player) => player.playerNo == 1).score = 1;
32
+
33
+
34
+ for(var detail in scoreEntity.detailList){
35
+ for(var player in detail.playerList){
36
+ print("holeNo:" + detail.holeNo.toString() + ", playerNo:" + player.playerNo.toString() +", name:" + player.playerName + ", score:" + player.score.toString());
37
+
38
+
39
+ }
40
+ }
41
+ }
42
+
43
+
13
44
  class ScoreEntity {
14
- final String roundId;
45
+
15
- int _currentHoleNo;
16
- final String courseName;
17
46
  List<Detail> detailList = [];
47
+
48
+ ScoreEntity();
18
49
 
19
- ScoreEntity(this.roundId, this._currentHoleNo, this.courseName) {
50
+ void addDetail(Detail detail) {
51
+ detailList.add(detail);
20
52
  }
53
+
54
+ // Getterメソッドを追加: currentHoleInfoプロパティ
55
+
56
+ }
57
+
21
58
  class Detail {
22
59
  int holeNo = 0;
23
60
  int par = 0;
@@ -37,24 +74,8 @@
37
74
  PlayerDetail(this.playerNo, this.playerName, this.score, this.putts, this.teeOrder);
38
75
  }
39
76
 
40
- //スコアを入力するページ
41
- void initState() {
42
- super.initState();
43
- //現在のホールNoを取得
44
- _currentHoleInfo = widget.scoreEntity.currentHoleInfo;
45
- //現在のホール情報を取得
46
- _detail = widget.scoreEntity.detailList[_currentHoleInfo.holeNo - 1];
47
- }
48
77
 
49
- //スコアの追加ボタンを押した時に呼ばれる関数(1づつカウントされる)
78
+
50
- void _updatePlayerScore(int targetPlayerNo, int change) {
51
- setState(() {
52
- _detail.playerList.firstWhere(
53
- (player) => player.playerNo == targetPlayerNo).score += change;
54
-
55
- });
56
- }
57
- ```
58
79
 
59
80
  ### 試したこと・調べたこと
60
81
  - [x] teratailやGoogle等で検索した
@@ -63,11 +84,12 @@
63
84
  - [ ] その他
64
85
 
65
86
  ##### 上記の詳細・結果
87
+ dart.padで実行できます
66
- ScoreEntityList<Detail> detailList = [];
88
+ holeNo:1playerNo:1のスコアだけ更新したいのですが
67
- は初期値とし18ホール分の初期データを設定います。
89
+ playerNo:1のスコアがすべ更新されています。
90
+ 解決方法はありますでしょうか?
91
+ よろしくお願いします。
68
92
 
69
- _updatePlayerScoreの関数が呼ばれる度にtargetPlayerNoの18ホール分プラス1される
70
- 1回のクリックで合計18のスコアが加算されてしまいます。
71
93
 
72
94
 
73
95
  ### 補足