質問するログイン新規登録

質問編集履歴

1

ErrorCodeと自動生成されたmodelを追加しました

2020/10/25 06:02

投稿

aru889
aru889

スコア1

title CHANGED
File without changes
body CHANGED
@@ -94,61 +94,134 @@
94
94
  }
95
95
  }
96
96
  ],
97
+ ```
98
+ ```model
99
+ class Autogenerated {
100
+ List<Data> data;
97
- "included": [
101
+ List<Included> included;
98
- {
102
+
99
- "id": "1",
100
- "type": "category",
103
+ Autogenerated({this.data, this.included});
104
+
105
+ Autogenerated.fromJson(Map<String, dynamic> json) {
101
- "attributes": {
106
+ if (json['data'] != null) {
102
- "name": "dev"
107
+ data = new List<Data>();
108
+ json['data'].forEach((v) {
109
+ data.add(new Data.fromJson(v));
110
+ });
103
- }
111
+ }
112
+ if (json['included'] != null) {
113
+ included = new List<Included>();
114
+ json['included'].forEach((v) {
115
+ included.add(new Included.fromJson(v));
104
- },
116
+ });
105
- {
106
- "id": "1",
107
- "type": "genre",
108
- "attributes": {
109
- "name": "dev"
110
- }
117
+ }
111
- },
112
- {
113
- "id": "1",
114
- "type": "other_user",
115
- "attributes": {
116
- "username": "user-dev",
117
- "introduction": null,
118
- "profileImagePath": null,
119
- "headerImagePath": null,
120
- "lastLoggedIn": "2019-10-04T14:39:19.560Z"
121
- }
118
+ }
122
- },
119
+
123
- {
124
- "id": "2",
125
- "type": "category",
120
+ Map<String, dynamic> toJson() {
121
+ final Map<String, dynamic> data = new Map<String, dynamic>();
126
- "attributes": {
122
+ if (this.data != null) {
127
- "name": "cate2"
123
+ data['data'] = this.data.map((v) => v.toJson()).toList();
128
- }
124
+ }
129
- },
130
- {
131
- "id": "2",
132
- "type": "genre",
133
- "attributes": {
125
+ if (this.included != null) {
134
- "name": "genre2"
126
+ data['included'] = this.included.map((v) => v.toJson()).toList();
135
- }
127
+ }
136
- },
137
- {
138
- "id": "3",
139
- "type": "other_user",
140
- "attributes": {
128
+ return data;
141
- "username": "test-user",
142
- "introduction": null,
143
- "profileImagePath": null,
144
- "headerImagePath": null,
145
- "lastLoggedIn": "2020-10-17T07:35:51.424Z"
146
- }
129
+ }
147
- }
148
- ]
149
130
  }
131
+
132
+ class Data {
133
+ String id;
134
+ String type;
135
+ Attributes attributes;
136
+ Relationships relationships;
137
+
138
+ Data({this.id, this.type, this.attributes, this.relationships});
139
+
140
+ Data.fromJson(Map<String, dynamic> json) {
141
+ id = json['id'];
142
+ type = json['type'];
143
+ attributes = json['attributes'] != null
144
+ ? new Attributes.fromJson(json['attributes'])
145
+ : null;
146
+ relationships = json['relationships'] != null
147
+ ? new Relationships.fromJson(json['relationships'])
148
+ : null;
149
+ }
150
+
151
+ Map<String, dynamic> toJson() {
152
+ final Map<String, dynamic> data = new Map<String, dynamic>();
153
+ data['id'] = this.id;
154
+ data['type'] = this.type;
155
+ if (this.attributes != null) {
156
+ data['attributes'] = this.attributes.toJson();
157
+ }
158
+ if (this.relationships != null) {
159
+ data['relationships'] = this.relationships.toJson();
160
+ }
161
+ return data;
162
+ }
163
+ }
164
+
165
+ class Attributes {
166
+ String title;
167
+ String description;
168
+ String imagePath;
169
+
170
+ Attributes({this.title, this.description, this.imagePath});
171
+
172
+ Attributes.fromJson(Map<String, dynamic> json) {
173
+ title = json['title'];
174
+ description = json['description'];
175
+ imagePath = json['image_path'];
176
+ }
177
+
178
+ Map<String, dynamic> toJson() {
179
+ final Map<String, dynamic> data = new Map<String, dynamic>();
180
+ data['title'] = this.title;
181
+ data['description'] = this.description;
182
+ data['image_path'] = this.imagePath;
183
+ return data;
184
+ }
185
+ }
186
+
187
+ class Attributes {
188
+ String name;
189
+ String username;
190
+ Null introduction;
191
+ Null profileImagePath;
192
+ Null headerImagePath;
193
+ String lastLoggedIn;
194
+
195
+ Attributes(
196
+ {this.name,
197
+ this.username,
198
+ this.introduction,
199
+ this.profileImagePath,
200
+ this.headerImagePath,
201
+ this.lastLoggedIn});
202
+
203
+ Attributes.fromJson(Map<String, dynamic> json) {
204
+ name = json['name'];
205
+ username = json['username'];
206
+ introduction = json['introduction'];
207
+ profileImagePath = json['profileImagePath'];
208
+ headerImagePath = json['headerImagePath'];
209
+ lastLoggedIn = json['lastLoggedIn'];
210
+ }
211
+
212
+ Map<String, dynamic> toJson() {
213
+ final Map<String, dynamic> data = new Map<String, dynamic>();
214
+ data['name'] = this.name;
215
+ data['username'] = this.username;
216
+ data['introduction'] = this.introduction;
217
+ data['profileImagePath'] = this.profileImagePath;
218
+ data['headerImagePath'] = this.headerImagePath;
219
+ data['lastLoggedIn'] = this.lastLoggedIn;
220
+ return data;
221
+ }
222
+ }
223
+
150
224
  ```
151
-
152
225
  ```apidart
153
226
  import 'dart:convert' show json, jsonDecode;
154
227
  import 'package:json_annotation/json_annotation.dart';
@@ -172,7 +245,7 @@
172
245
  //flutter: {title: room2, description: room2 description, image_path: jiroewa}
173
246
 
174
247
 
175
- return new Room.fromJson(res);
248
+ return new Attributes.fromJson(res);
176
249
  }
177
250
  }
178
251
 
@@ -215,4 +288,7 @@
215
288
  という感じです。
216
289
  現状、JSONをデコードしてくるのは出来るんですが配列?に変換するところが出来ていないのでmain.dartで取得できず、error表示という認識でいます。
217
290
 
291
+ ```ErrorCode
292
+ Error: The argument type 'List<dynamic>' can't be assigned to the parameter type
218
- こちらを参考にしていたのですが解決できず、、お力添えいただけると助かります。
293
+ 'Map<String, dynamic>'.
294
+ ```