やりたいことは簡単なのですがJSONが少々複雑で難しいです…><
よくあるAPIから値をGETしてきてFutureBuilderでdata表示するものがほしいです。
modelは https://javiercbk.github.io/json_to_dart/ でジェネレートしたものを使ってるのですがよくわからずで。。
APIからGETしてくるJSONがこちらで。
{ "data": [ { "id": "1", "type": "room", "attributes": { "title": "room1", "description": "room1 description", "image_path": "path" }, "relationships": { "user": { "data": { "id": "1", "type": "user" } }, "category": { "data": { "id": "1", "type": "category" } }, "genre": { "data": { "id": "1", "type": "genre" } } } }, { "id": "2", "type": "room", "attributes": { "title": "room2", "description": "room2 description", "image_path": "path" }, "relationships": { "user": { "data": { "id": "3", "type": "user" } }, "category": { "data": { "id": "2", "type": "category" } }, "genre": { "data": { "id": "2", "type": "genre" } } } }, { "id": "3", "type": "room", "attributes": { "title": "room3", "description": "room3 description", "image_path": "path" }, "relationships": { "user": { "data": { "id": "3", "type": "user" } }, "category": { "data": { "id": "2", "type": "category" } }, "genre": { "data": { "id": "2", "type": "genre" } } } } ],
model
1class Autogenerated { 2 List<Data> data; 3 List<Included> included; 4 5 Autogenerated({this.data, this.included}); 6 7 Autogenerated.fromJson(Map<String, dynamic> json) { 8 if (json['data'] != null) { 9 data = new List<Data>(); 10 json['data'].forEach((v) { 11 data.add(new Data.fromJson(v)); 12 }); 13 } 14 if (json['included'] != null) { 15 included = new List<Included>(); 16 json['included'].forEach((v) { 17 included.add(new Included.fromJson(v)); 18 }); 19 } 20 } 21 22 Map<String, dynamic> toJson() { 23 final Map<String, dynamic> data = new Map<String, dynamic>(); 24 if (this.data != null) { 25 data['data'] = this.data.map((v) => v.toJson()).toList(); 26 } 27 if (this.included != null) { 28 data['included'] = this.included.map((v) => v.toJson()).toList(); 29 } 30 return data; 31 } 32} 33 34class Data { 35 String id; 36 String type; 37 Attributes attributes; 38 Relationships relationships; 39 40 Data({this.id, this.type, this.attributes, this.relationships}); 41 42 Data.fromJson(Map<String, dynamic> json) { 43 id = json['id']; 44 type = json['type']; 45 attributes = json['attributes'] != null 46 ? new Attributes.fromJson(json['attributes']) 47 : null; 48 relationships = json['relationships'] != null 49 ? new Relationships.fromJson(json['relationships']) 50 : null; 51 } 52 53 Map<String, dynamic> toJson() { 54 final Map<String, dynamic> data = new Map<String, dynamic>(); 55 data['id'] = this.id; 56 data['type'] = this.type; 57 if (this.attributes != null) { 58 data['attributes'] = this.attributes.toJson(); 59 } 60 if (this.relationships != null) { 61 data['relationships'] = this.relationships.toJson(); 62 } 63 return data; 64 } 65} 66 67class Attributes { 68 String title; 69 String description; 70 String imagePath; 71 72 Attributes({this.title, this.description, this.imagePath}); 73 74 Attributes.fromJson(Map<String, dynamic> json) { 75 title = json['title']; 76 description = json['description']; 77 imagePath = json['image_path']; 78 } 79 80 Map<String, dynamic> toJson() { 81 final Map<String, dynamic> data = new Map<String, dynamic>(); 82 data['title'] = this.title; 83 data['description'] = this.description; 84 data['image_path'] = this.imagePath; 85 return data; 86 } 87} 88 89class Attributes { 90 String name; 91 String username; 92 Null introduction; 93 Null profileImagePath; 94 Null headerImagePath; 95 String lastLoggedIn; 96 97 Attributes( 98 {this.name, 99 this.username, 100 this.introduction, 101 this.profileImagePath, 102 this.headerImagePath, 103 this.lastLoggedIn}); 104 105 Attributes.fromJson(Map<String, dynamic> json) { 106 name = json['name']; 107 username = json['username']; 108 introduction = json['introduction']; 109 profileImagePath = json['profileImagePath']; 110 headerImagePath = json['headerImagePath']; 111 lastLoggedIn = json['lastLoggedIn']; 112 } 113 114 Map<String, dynamic> toJson() { 115 final Map<String, dynamic> data = new Map<String, dynamic>(); 116 data['name'] = this.name; 117 data['username'] = this.username; 118 data['introduction'] = this.introduction; 119 data['profileImagePath'] = this.profileImagePath; 120 data['headerImagePath'] = this.headerImagePath; 121 data['lastLoggedIn'] = this.lastLoggedIn; 122 return data; 123 } 124} 125
apidart
1import 'dart:convert' show json, jsonDecode; 2import 'package:json_annotation/json_annotation.dart'; 3import 'package:http/http.dart' as http; 4import 'package:api_test/models/room.dart'; 5 6@JsonSerializable() 7 8class WebService { 9 static const baseurl = '***api url***'; 10 Future fetchRoom() async { 11 final response = await http.get('${baseurl}rooms'); 12 if (response.statusCode == 200) { 13 final res = json.decode(response.body)['data'] as List; 14 final res2 = json.decode(response.body)['data'][0]['attributes'] as Map; 15 final res3 = json.decode(response.body)['data'][1]['attributes'] as Map; 16 17 print(res2); 18//flutter: {title: room1, description: room1 description, image_path: jiroewa} 19 print(res3); 20//flutter: {title: room2, description: room2 description, image_path: jiroewa} 21 22 23 return new Attributes.fromJson(res); 24 } 25 } 26
homedart
1 2 body: Center( 3 child: Column(children: [ 4 FutureBuilder<dynamic>( 5 future: WebService().fetchRoom(), 6 builder: (context, snapshot) { 7 if (snapshot.hasData) { 8 return Column( 9 children: [ 10 Text(snapshot.data.title.toString()), 11 ], 12 ); 13 } else if (snapshot.connectionState == 14 ConnectionState.waiting) { 15 return const Center( 16 child: Text('waiting'), 17 ); 18 // ignore: invariant_booleans 19 } else if (snapshot.data == null) { 20 return Center( 21 child: Text(snapshot.data.toString()), 22 ); 23 } else { 24 return const Center( 25 child: CircularProgressIndicator(), 26 ); 27 } 28 }, 29 ), 30 ])) 31
という感じです。
現状、JSONをデコードしてくるのは出来るんですが配列?に変換するところが出来ていないのでmain.dartで取得できず、error表示という認識でいます。
ErrorCode
1Error: The argument type 'List<dynamic>' can't be assigned to the parameter type 2'Map<String, dynamic>'.
回答1件
あなたの回答
tips
プレビュー