[英]Flutter / Dart - JSON list to display in widget - shows only one value
flutter 和 dart 的新手,正在尝试编写我的第一个应用程序。 我正在从有效的 API 中提取 JSON 数据。
JSON 响应如下:
{
"meta": {
"description": "OK",
"message": "OK",
"code": 200
},
"data": [
{
"match_no": 0,
"entries": [
{
"item": {
"no": "2432",
"name": "Tile, Modified 1 x 2 with Bar Handle",
"type": "PART",
"category_id": 38
},
"color_id": 3,
"quantity": 1,
"extra_quantity": 0,
"is_alternate": false,
"is_counterpart": false
}
]
},
{
"match_no": 0,
"entries": [
{
"item": {
"no": "3005",
"name": "Brick 1 x 1",
"type": "PART",
"category_id": 5
},
"color_id": 5,
"quantity": 1,
"extra_quantity": 0,
"is_alternate": false,
"is_counterpart": false
}
]
},
{
"match_no": 0,
"entries": [
{
"item": {
"no": "3022",
"name": "Plate 2 x 2",
"type": "PART",
"category_id": 26
},
"color_id": 85,
"quantity": 3,
"extra_quantity": 0,
"is_alternate": false,
"is_counterpart": false
}
]
},
{
"match_no": 0,
"entries": [
{
"item": {
"no": "3024",
"name": "Plate 1 x 1",
"type": "PART",
"category_id": 26
},
"color_id": 3,
"quantity": 4,
"extra_quantity": 1,
"is_alternate": false,
"is_counterpart": false
}
]
},
{
"match_no": 0,
"entries": [
{
"item": {
"no": "3069bpx7",
"name": "Tile 1 x 2 with Groove with White 100 Paper Bill Money Pattern",
"type": "PART",
"category_id": 39
},
"color_id": 6,
"quantity": 4,
"extra_quantity": 0,
"is_alternate": false,
"is_counterpart": false
}
]
},
{
"match_no": 0,
"entries": [
{
"item": {
"no": "4733",
"name": "Brick, Modified 1 x 1 with Studs on 4 Sides",
"type": "PART",
"category_id": 7
},
"color_id": 85,
"quantity": 2,
"extra_quantity": 0,
"is_alternate": false,
"is_counterpart": false
}
]
},
{
"match_no": 0,
"entries": [
{
"item": {
"no": "4740",
"name": "Dish 2 x 2 Inverted (Radar)",
"type": "PART",
"category_id": 116
},
"color_id": 3,
"quantity": 2,
"extra_quantity": 0,
"is_alternate": false,
"is_counterpart": false
}
]
},
{
"match_no": 0,
"entries": [
{
"item": {
"no": "4871",
"name": "Slope, Inverted 45 4 x 2 Double with 2 x 2 Cutout",
"type": "PART",
"category_id": 32
},
"color_id": 3,
"quantity": 1,
"extra_quantity": 0,
"is_alternate": false,
"is_counterpart": false
}
]
},
{
"match_no": 0,
"entries": [
{
"item": {
"no": "30663",
"name": "Vehicle, Steering Wheel Small, 2 Studs Diameter",
"type": "PART",
"category_id": 72
},
"color_id": 86,
"quantity": 2,
"extra_quantity": 0,
"is_alternate": false,
"is_counterpart": false
}
]
},
{
"match_no": 0,
"entries": [
{
"item": {
"no": "43898",
"name": "Dish 3 x 3 Inverted (Radar)",
"type": "PART",
"category_id": 116
},
"color_id": 11,
"quantity": 1,
"extra_quantity": 0,
"is_alternate": false,
"is_counterpart": false
}
]
},
{
"match_no": 0,
"entries": [
{
"item": {
"no": "4697b",
"name": "Pneumatic T Piece Second Version (T Bar with Ball in Center)",
"type": "PART",
"category_id": 159
},
"color_id": 86,
"quantity": 4,
"extra_quantity": 1,
"is_alternate": false,
"is_counterpart": false
}
]
},
{
"match_no": 0,
"entries": [
{
"item": {
"no": "44674",
"name": "Vehicle, Mudguard 2 x 4 with Headlights Overhang",
"type": "PART",
"category_id": 77
},
"color_id": 3,
"quantity": 2,
"extra_quantity": 0,
"is_alternate": false,
"is_counterpart": false
}
]
},
{
"match_no": 0,
"entries": [
{
"item": {
"no": "98721",
"name": "Minifigure, Weapon Batman Batarang (2 Bat Wings with Bar in Middle)",
"type": "PART",
"category_id": 19
},
"color_id": 11,
"quantity": 4,
"extra_quantity": 1,
"is_alternate": false,
"is_counterpart": false
}
]
},
{
"match_no": 0,
"entries": [
{
"item": {
"no": "61780",
"name": "Container, Box 2 x 2 x 2 - Top Opening",
"type": "PART",
"category_id": 10
},
"color_id": 85,
"quantity": 2,
"extra_quantity": 0,
"is_alternate": false,
"is_counterpart": false
}
]
},
{
"match_no": 0,
"entries": [
{
"item": {
"no": "64644",
"name": "Minifigure, Utensil Telescope",
"type": "PART",
"category_id": 18
},
"color_id": 120,
"quantity": 2,
"extra_quantity": 0,
"is_alternate": false,
"is_counterpart": false
}
]
},
{
"match_no": 0,
"entries": [
{
"item": {
"no": "64728",
"name": "Minifigure, Utensil Dynamite Sticks Bundle",
"type": "PART",
"category_id": 18
},
"color_id": 5,
"quantity": 5,
"extra_quantity": 0,
"is_alternate": false,
"is_counterpart": false
}
]
},
{
"match_no": 0,
"entries": [
{
"item": {
"no": "85861",
"name": "Plate, Round 1 x 1 with Open Stud",
"type": "PART",
"category_id": 28
},
"color_id": 5,
"quantity": 4,
"extra_quantity": 1,
"is_alternate": false,
"is_counterpart": false
}
]
},
{
"match_no": 0,
"entries": [
{
"item": {
"no": "92280",
"name": "Plate, Modified 1 x 2 with Clip on Top",
"type": "PART",
"category_id": 27
},
"color_id": 3,
"quantity": 2,
"extra_quantity": 0,
"is_alternate": false,
"is_counterpart": false
}
]
},
{
"match_no": 0,
"entries": [
{
"item": {
"no": "98138pb007",
"name": "Tile, Round 1 x 1 with Black Eye with Pupil Pattern",
"type": "PART",
"category_id": 812
},
"color_id": 1,
"quantity": 3,
"extra_quantity": 1,
"is_alternate": false,
"is_counterpart": false
}
]
},
{
"match_no": 0,
"entries": [
{
"item": {
"no": "98100",
"name": "Cone 2 x 2 Truncated",
"type": "PART",
"category_id": 21
},
"color_id": 69,
"quantity": 2,
"extra_quantity": 0,
"is_alternate": false,
"is_counterpart": false
}
]
},
{
"match_no": 0,
"entries": [
{
"item": {
"no": "11476",
"name": "Plate, Modified 1 x 2 with Clip on Side (Horizontal Grip)",
"type": "PART",
"category_id": 27
},
"color_id": 3,
"quantity": 2,
"extra_quantity": 0,
"is_alternate": false,
"is_counterpart": false
}
]
},
{
"match_no": 0,
"entries": [
{
"item": {
"no": "85984pb127",
"name": "Slope 30 1 x 2 x 2/3 with Red, White, and Silver Buttons, Dark Red Screens Pattern",
"type": "PART",
"category_id": 33
},
"color_id": 85,
"quantity": 1,
"extra_quantity": 0,
"is_alternate": false,
"is_counterpart": false
}
]
},
{
"match_no": 0,
"entries": [
{
"item": {
"no": "35464",
"name": "Slope 45 1 x 1 Double",
"type": "PART",
"category_id": 31
},
"color_id": 4,
"quantity": 2,
"extra_quantity": 1,
"is_alternate": false,
"is_counterpart": false
}
]
},
{
"match_no": 0,
"entries": [
{
"item": {
"no": "sh647",
"name": "The Penguin - Bright Waistcoat",
"type": "MINIFIG",
"category_id": 768
},
"color_id": 0,
"quantity": 1,
"extra_quantity": 0,
"is_alternate": false,
"is_counterpart": false
}
]
},
{
"match_no": 0,
"entries": [
{
"item": {
"no": "sh650",
"name": "Harley Quinn - Jacket Open, Corset",
"type": "MINIFIG",
"category_id": 768
},
"color_id": 0,
"quantity": 1,
"extra_quantity": 0,
"is_alternate": false,
"is_counterpart": false
}
]
},
{
"match_no": 0,
"entries": [
{
"item": {
"no": "sh689",
"name": "Batman - Light Bluish Gray Suit with Yellow Belt, Black Crest, Mask and Cape (Type 3 Cowl)",
"type": "MINIFIG",
"category_id": 768
},
"color_id": 0,
"quantity": 1,
"extra_quantity": 0,
"is_alternate": false,
"is_counterpart": false
}
]
},
{
"match_no": 0,
"entries": [
{
"item": {
"no": "40453-1",
"name": "Batman vs. The Penguin & Harley Quinn blister pack",
"type": "INSTRUCTION",
"category_id": 768
},
"color_id": 0,
"quantity": 1,
"extra_quantity": 0,
"is_alternate": false,
"is_counterpart": false
}
]
}
]
}
然后我使用 quicktype.io 为自己获取 dart 代码 - Bricklink.dart:
// To parse this JSON data, do
//
// final bricklink = bricklinkFromJson(jsonString);
import 'package:meta/meta.dart';
import 'dart:convert';
Bricklink bricklinkFromJson(String str) => Bricklink.fromJson(json.decode(str));
String bricklinkToJson(Bricklink data) => json.encode(data.toJson());
class Bricklink {
Bricklink({
required this.meta,
required this.data,
});
Meta meta;
List<Datum> data;
factory Bricklink.fromJson(Map<String, dynamic> json) => Bricklink(
meta: Meta.fromJson(json["meta"]),
data: List<Datum>.from(json["data"].map((x) => Datum.fromJson(x))),
);
get length => null;
Map<String, dynamic> toJson() => {
"meta": meta.toJson(),
"data": List<dynamic>.from(data.map((x) => x.toJson())),
};
}
class Datum {
Datum({
required this.matchNo,
required this.entries,
});
int matchNo;
List<Entry> entries;
factory Datum.fromJson(Map<String, dynamic> json) => Datum(
matchNo: json["match_no"],
entries: List<Entry>.from(json["entries"].map((x) => Entry.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"match_no": matchNo,
"entries": List<dynamic>.from(entries.map((x) => x.toJson())),
};
}
class Entry {
Entry({
required this.item,
required this.colorId,
required this.quantity,
required this.extraQuantity,
required this.isAlternate,
required this.isCounterpart,
});
Item item;
int colorId;
int quantity;
int extraQuantity;
bool isAlternate;
bool isCounterpart;
factory Entry.fromJson(Map<String, dynamic> json) => Entry(
item: Item.fromJson(json["item"]),
colorId: json["color_id"],
quantity: json["quantity"],
extraQuantity: json["extra_quantity"],
isAlternate: json["is_alternate"],
isCounterpart: json["is_counterpart"],
);
Map<String, dynamic> toJson() => {
"item": item.toJson(),
"color_id": colorId,
"quantity": quantity,
"extra_quantity": extraQuantity,
"is_alternate": isAlternate,
"is_counterpart": isCounterpart,
};
}
class Item {
Item({
required this.no,
required this.name,
//required this.type,
required this.categoryId,
});
String no;
String name;
//Type type;
int categoryId;
factory Item.fromJson(Map<String, dynamic> json) => Item(
no: json["no"],
name: json["name"],
//type: typeValues.map[json["type"]],
categoryId: json["category_id"],
);
Map<String, dynamic> toJson() => {
"no": no,
"name": name,
//"type": typeValues.reverse[type],
"category_id": categoryId,
};
}
enum Type { PART, MINIFIG, INSTRUCTION }
final typeValues = EnumValues({
"INSTRUCTION": Type.INSTRUCTION,
"MINIFIG": Type.MINIFIG,
"PART": Type.PART
});
class Meta {
Meta({
required this.description,
required this.message,
required this.code,
});
String description;
String message;
int code;
factory Meta.fromJson(Map<String, dynamic> json) => Meta(
description: json["description"],
message: json["message"],
code: json["code"],
);
Map<String, dynamic> toJson() => {
"description": description,
"message": message,
"code": code,
};
}
class EnumValues<T> {
Map<String, T> map;
late Map<T, String> reverseMap;
EnumValues(this.map);
Map<T, String> get reverse {
if (reverseMap == null) {
reverseMap = map.map((k, v) => new MapEntry(v, k));
}
return reverseMap;
}
}
按照其他一些指南,我能够创建一个显示列表的 flutter 应用程序。 该列表与 JSON output 中的项目数对齐(在上面的示例中为 27 行),而它仅显示所有 27 行中的请求项目,而不是完整列表。
我的进一步代码:
主要.dart:
import 'package:flutter/material.dart';
import 'JsonParseDemo.dart';
import 'package:http/http.dart' as http;
void main () {
runApp(HomeApp());
}
class HomeApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: JsonParseDemo(),
);
}
}
服务.dart:
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'Bricklink.dart';
class Services {
//
static const String url = 'https://api.bricklink.com/api/store/v1/items/set/40453-1/subsets?instruction=true&break_minifigs=false&break_subsets=true';
static Future<Bricklink> getUsers() async {
try {
final response = await http.get(Uri.parse(url), headers: {
'Authorization':
'HIDDEN',
'Cookie':
'HIDDEN'
});
if (200 == response.statusCode) {
Bricklink users = jsonDecode(response.body);
return users;
} else {
throw Exception('Failed To Load Data');
}
} catch (e) {
throw Exception('Failed To Load Data');
}
}
}
最后一块包括小部件 - JsonParseDemo.dart:
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'Services.dart';
import 'Users.dart';
import 'Bricklink.dart';
class _JsonParseDemoState extends State<JsonParseDemo> {
Bricklink? _bricklink;
var _loading = false;
@override
void initState() {
super.initState();
_loading = true;
Services.getUsers().then((bricklink) {
setState(() {
_bricklink = bricklink;
_loading = false;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(_loading ? 'Loading...' : 'BrickLink'),
),
body: Container(
color: Colors.white,
child: ListView.builder(
itemCount: null == _bricklink ? 0 : _bricklink!.length,
itemBuilder: (context, index) {
Bricklink bricklink = _bricklink!;
return ListTile(
title: Text(bricklink.data[3].entries[0].item.no),
subtitle: Text(bricklink.data[3].entries[0].item.name),
leading: CircleAvatar(child: Text(bricklink.data[3].entries[0].quantity.toString())),
);
},
),
),
);
}
}
class JsonParseDemo extends StatefulWidget {
//
JsonParseDemo() : super();
@override
_JsonParseDemoState createState() => _JsonParseDemoState();
}
我明白我使用
title: Text(bricklink.data[3].entries[0].item.no),
subtitle: Text(bricklink.data[3].entries[0].item.name),
结果只拉出 JSON 响应中的第 4 个条目,但我如何将完整列表拉入小部件?
感谢任何指针。
谢谢!
在数据数组中添加索引
bricklink.data[index].entries[0]
ListWidget
ListView.builder(
itemCount: null == _bricklink ? 0 : _bricklink!.length,
itemBuilder: (context, index) {
Bricklink bricklink = _bricklink!;
return ListTile(
title: Text(bricklink.data[index].entries[0].item.no),
subtitle: Text(bricklink.data[index].entries[0].item.name),
leading: CircleAvatar(
child:
Text(bricklink.data[index].entries[0].quantity.toString())),
);
},
)
示例代码
// To parse this JSON data, do
//
// final bricklink = bricklinkFromJson(jsonString);
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
Bricklink bricklinkFromJson(String str) => Bricklink.fromJson(json.decode(str));
String bricklinkToJson(Bricklink data) => json.encode(data.toJson());
class Bricklink {
Bricklink({
required this.meta,
required this.data,
});
Meta meta;
List<Datum> data;
factory Bricklink.fromJson(Map<String, dynamic> json) => Bricklink(
meta: Meta.fromJson(json["meta"]),
data: List<Datum>.from(json["data"].map((x) => Datum.fromJson(x))),
);
get length => null;
Map<String, dynamic> toJson() => {
"meta": meta.toJson(),
"data": List<dynamic>.from(data.map((x) => x.toJson())),
};
}
class Datum {
Datum({
required this.matchNo,
required this.entries,
});
int matchNo;
List<Entry> entries;
factory Datum.fromJson(Map<String, dynamic> json) => Datum(
matchNo: json["match_no"],
entries:
List<Entry>.from(json["entries"].map((x) => Entry.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"match_no": matchNo,
"entries": List<dynamic>.from(entries.map((x) => x.toJson())),
};
}
class Entry {
Entry({
required this.item,
required this.colorId,
required this.quantity,
required this.extraQuantity,
required this.isAlternate,
required this.isCounterpart,
});
Item item;
int colorId;
int quantity;
int extraQuantity;
bool isAlternate;
bool isCounterpart;
factory Entry.fromJson(Map<String, dynamic> json) => Entry(
item: Item.fromJson(json["item"]),
colorId: json["color_id"],
quantity: json["quantity"],
extraQuantity: json["extra_quantity"],
isAlternate: json["is_alternate"],
isCounterpart: json["is_counterpart"],
);
Map<String, dynamic> toJson() => {
"item": item.toJson(),
"color_id": colorId,
"quantity": quantity,
"extra_quantity": extraQuantity,
"is_alternate": isAlternate,
"is_counterpart": isCounterpart,
};
}
class Item {
Item({
required this.no,
required this.name,
//required this.type,
required this.categoryId,
});
String no;
String name;
//Type type;
int categoryId;
factory Item.fromJson(Map<String, dynamic> json) => Item(
no: json["no"],
name: json["name"],
//type: typeValues.map[json["type"]],
categoryId: json["category_id"],
);
Map<String, dynamic> toJson() => {
"no": no,
"name": name,
//"type": typeValues.reverse[type],
"category_id": categoryId,
};
}
enum Type { PART, MINIFIG, INSTRUCTION }
final typeValues = EnumValues({
"INSTRUCTION": Type.INSTRUCTION,
"MINIFIG": Type.MINIFIG,
"PART": Type.PART
});
class Meta {
Meta({
required this.description,
required this.message,
required this.code,
});
String description;
String message;
int code;
factory Meta.fromJson(Map<String, dynamic> json) => Meta(
description: json["description"],
message: json["message"],
code: json["code"],
);
Map<String, dynamic> toJson() => {
"description": description,
"message": message,
"code": code,
};
}
class EnumValues<T> {
Map<String, T> map;
late Map<T, String> reverseMap;
EnumValues(this.map);
Map<T, String> get reverse {
if (reverseMap == null) {
reverseMap = map.map((k, v) => new MapEntry(v, k));
}
return reverseMap;
}
}
void main() {
runApp(HomeApp());
}
class HomeApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: JsonParseDemo(),
);
}
}
class Services {
//
static const String url =
'https://api.bricklink.com/api/store/v1/items/set/40453-1/subsets?instruction=true&break_minifigs=false&break_subsets=true';
static Future<Bricklink> getUsers() async {
try {
final response = await get(Uri.parse(url),
headers: {'Authorization': 'HIDDEN', 'Cookie': 'HIDDEN'});
if (200 == response.statusCode) {
Bricklink users = jsonDecode(
'{ "meta": { "description": "OK", "message": "OK", "code": 200 }, "data": [ { "match_no": 0, "entries": [ { "item": { "no": "2432", "name": "Tile, Modified 1 x 2 with Bar Handle", "type": "PART", "category_id": 38 }, "color_id": 3, "quantity": 1, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "3005", "name": "Brick 1 x 1", "type": "PART", "category_id": 5 }, "color_id": 5, "quantity": 1, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "3022", "name": "Plate 2 x 2", "type": "PART", "category_id": 26 }, "color_id": 85, "quantity": 3, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "3024", "name": "Plate 1 x 1", "type": "PART", "category_id": 26 }, "color_id": 3, "quantity": 4, "extra_quantity": 1, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "3069bpx7", "name": "Tile 1 x 2 with Groove with White 100 Paper Bill Money Pattern", "type": "PART", "category_id": 39 }, "color_id": 6, "quantity": 4, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "4733", "name": "Brick, Modified 1 x 1 with Studs on 4 Sides", "type": "PART", "category_id": 7 }, "color_id": 85, "quantity": 2, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "4740", "name": "Dish 2 x 2 Inverted (Radar)", "type": "PART", "category_id": 116 }, "color_id": 3, "quantity": 2, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "4871", "name": "Slope, Inverted 45 4 x 2 Double with 2 x 2 Cutout", "type": "PART", "category_id": 32 }, "color_id": 3, "quantity": 1, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "30663", "name": "Vehicle, Steering Wheel Small, 2 Studs Diameter", "type": "PART", "category_id": 72 }, "color_id": 86, "quantity": 2, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "43898", "name": "Dish 3 x 3 Inverted (Radar)", "type": "PART", "category_id": 116 }, "color_id": 11, "quantity": 1, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "4697b", "name": "Pneumatic T Piece Second Version (T Bar with Ball in Center)", "type": "PART", "category_id": 159 }, "color_id": 86, "quantity": 4, "extra_quantity": 1, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "44674", "name": "Vehicle, Mudguard 2 x 4 with Headlights Overhang", "type": "PART", "category_id": 77 }, "color_id": 3, "quantity": 2, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "98721", "name": "Minifigure, Weapon Batman Batarang (2 Bat Wings with Bar in Middle)", "type": "PART", "category_id": 19 }, "color_id": 11, "quantity": 4, "extra_quantity": 1, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "61780", "name": "Container, Box 2 x 2 x 2 - Top Opening", "type": "PART", "category_id": 10 }, "color_id": 85, "quantity": 2, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "64644", "name": "Minifigure, Utensil Telescope", "type": "PART", "category_id": 18 }, "color_id": 120, "quantity": 2, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "64728", "name": "Minifigure, Utensil Dynamite Sticks Bundle", "type": "PART", "category_id": 18 }, "color_id": 5, "quantity": 5, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "85861", "name": "Plate, Round 1 x 1 with Open Stud", "type": "PART", "category_id": 28 }, "color_id": 5, "quantity": 4, "extra_quantity": 1, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "92280", "name": "Plate, Modified 1 x 2 with Clip on Top", "type": "PART", "category_id": 27 }, "color_id": 3, "quantity": 2, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "98138pb007", "name": "Tile, Round 1 x 1 with Black Eye with Pupil Pattern", "type": "PART", "category_id": 812 }, "color_id": 1, "quantity": 3, "extra_quantity": 1, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "98100", "name": "Cone 2 x 2 Truncated", "type": "PART", "category_id": 21 }, "color_id": 69, "quantity": 2, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "11476", "name": "Plate, Modified 1 x 2 with Clip on Side (Horizontal Grip)", "type": "PART", "category_id": 27 }, "color_id": 3, "quantity": 2, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "85984pb127", "name": "Slope 30 1 x 2 x 2/3 with Red, White, and Silver Buttons, Dark Red Screens Pattern", "type": "PART", "category_id": 33 }, "color_id": 85, "quantity": 1, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "35464", "name": "Slope 45 1 x 1 Double", "type": "PART", "category_id": 31 }, "color_id": 4, "quantity": 2, "extra_quantity": 1, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "sh647", "name": "The Penguin - Bright Waistcoat", "type": "MINIFIG", "category_id": 768 }, "color_id": 0, "quantity": 1, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "sh650", "name": "Harley Quinn - Jacket Open, Corset", "type": "MINIFIG", "category_id": 768 }, "color_id": 0, "quantity": 1, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "sh689", "name": "Batman - Light Bluish Gray Suit with Yellow Belt, Black Crest, Mask and Cape (Type 3 Cowl)", "type": "MINIFIG", "category_id": 768 }, "color_id": 0, "quantity": 1, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "40453-1", "name": "Batman vs. The Penguin & Harley Quinn blister pack", "type": "INSTRUCTION", "category_id": 768 }, "color_id": 0, "quantity": 1, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] } ] }');
return users;
} else {
Bricklink users = jsonDecode(
'{ "meta": { "description": "OK", "message": "OK", "code": 200 }, "data": [ { "match_no": 0, "entries": [ { "item": { "no": "2432", "name": "Tile, Modified 1 x 2 with Bar Handle", "type": "PART", "category_id": 38 }, "color_id": 3, "quantity": 1, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "3005", "name": "Brick 1 x 1", "type": "PART", "category_id": 5 }, "color_id": 5, "quantity": 1, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "3022", "name": "Plate 2 x 2", "type": "PART", "category_id": 26 }, "color_id": 85, "quantity": 3, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "3024", "name": "Plate 1 x 1", "type": "PART", "category_id": 26 }, "color_id": 3, "quantity": 4, "extra_quantity": 1, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "3069bpx7", "name": "Tile 1 x 2 with Groove with White 100 Paper Bill Money Pattern", "type": "PART", "category_id": 39 }, "color_id": 6, "quantity": 4, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "4733", "name": "Brick, Modified 1 x 1 with Studs on 4 Sides", "type": "PART", "category_id": 7 }, "color_id": 85, "quantity": 2, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "4740", "name": "Dish 2 x 2 Inverted (Radar)", "type": "PART", "category_id": 116 }, "color_id": 3, "quantity": 2, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "4871", "name": "Slope, Inverted 45 4 x 2 Double with 2 x 2 Cutout", "type": "PART", "category_id": 32 }, "color_id": 3, "quantity": 1, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "30663", "name": "Vehicle, Steering Wheel Small, 2 Studs Diameter", "type": "PART", "category_id": 72 }, "color_id": 86, "quantity": 2, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "43898", "name": "Dish 3 x 3 Inverted (Radar)", "type": "PART", "category_id": 116 }, "color_id": 11, "quantity": 1, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "4697b", "name": "Pneumatic T Piece Second Version (T Bar with Ball in Center)", "type": "PART", "category_id": 159 }, "color_id": 86, "quantity": 4, "extra_quantity": 1, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "44674", "name": "Vehicle, Mudguard 2 x 4 with Headlights Overhang", "type": "PART", "category_id": 77 }, "color_id": 3, "quantity": 2, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "98721", "name": "Minifigure, Weapon Batman Batarang (2 Bat Wings with Bar in Middle)", "type": "PART", "category_id": 19 }, "color_id": 11, "quantity": 4, "extra_quantity": 1, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "61780", "name": "Container, Box 2 x 2 x 2 - Top Opening", "type": "PART", "category_id": 10 }, "color_id": 85, "quantity": 2, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "64644", "name": "Minifigure, Utensil Telescope", "type": "PART", "category_id": 18 }, "color_id": 120, "quantity": 2, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "64728", "name": "Minifigure, Utensil Dynamite Sticks Bundle", "type": "PART", "category_id": 18 }, "color_id": 5, "quantity": 5, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "85861", "name": "Plate, Round 1 x 1 with Open Stud", "type": "PART", "category_id": 28 }, "color_id": 5, "quantity": 4, "extra_quantity": 1, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "92280", "name": "Plate, Modified 1 x 2 with Clip on Top", "type": "PART", "category_id": 27 }, "color_id": 3, "quantity": 2, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "98138pb007", "name": "Tile, Round 1 x 1 with Black Eye with Pupil Pattern", "type": "PART", "category_id": 812 }, "color_id": 1, "quantity": 3, "extra_quantity": 1, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "98100", "name": "Cone 2 x 2 Truncated", "type": "PART", "category_id": 21 }, "color_id": 69, "quantity": 2, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "11476", "name": "Plate, Modified 1 x 2 with Clip on Side (Horizontal Grip)", "type": "PART", "category_id": 27 }, "color_id": 3, "quantity": 2, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "85984pb127", "name": "Slope 30 1 x 2 x 2/3 with Red, White, and Silver Buttons, Dark Red Screens Pattern", "type": "PART", "category_id": 33 }, "color_id": 85, "quantity": 1, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "35464", "name": "Slope 45 1 x 1 Double", "type": "PART", "category_id": 31 }, "color_id": 4, "quantity": 2, "extra_quantity": 1, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "sh647", "name": "The Penguin - Bright Waistcoat", "type": "MINIFIG", "category_id": 768 }, "color_id": 0, "quantity": 1, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "sh650", "name": "Harley Quinn - Jacket Open, Corset", "type": "MINIFIG", "category_id": 768 }, "color_id": 0, "quantity": 1, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "sh689", "name": "Batman - Light Bluish Gray Suit with Yellow Belt, Black Crest, Mask and Cape (Type 3 Cowl)", "type": "MINIFIG", "category_id": 768 }, "color_id": 0, "quantity": 1, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "40453-1", "name": "Batman vs. The Penguin & Harley Quinn blister pack", "type": "INSTRUCTION", "category_id": 768 }, "color_id": 0, "quantity": 1, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] } ] }');
return users;
}
} catch (e) {
Bricklink users = Bricklink.fromJson(jsonDecode(
'{ "meta": { "description": "OK", "message": "OK", "code": 200 }, "data": [ { "match_no": 0, "entries": [ { "item": { "no": "2432", "name": "Tile, Modified 1 x 2 with Bar Handle", "type": "PART", "category_id": 38 }, "color_id": 3, "quantity": 1, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "3005", "name": "Brick 1 x 1", "type": "PART", "category_id": 5 }, "color_id": 5, "quantity": 1, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "3022", "name": "Plate 2 x 2", "type": "PART", "category_id": 26 }, "color_id": 85, "quantity": 3, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "3024", "name": "Plate 1 x 1", "type": "PART", "category_id": 26 }, "color_id": 3, "quantity": 4, "extra_quantity": 1, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "3069bpx7", "name": "Tile 1 x 2 with Groove with White 100 Paper Bill Money Pattern", "type": "PART", "category_id": 39 }, "color_id": 6, "quantity": 4, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "4733", "name": "Brick, Modified 1 x 1 with Studs on 4 Sides", "type": "PART", "category_id": 7 }, "color_id": 85, "quantity": 2, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "4740", "name": "Dish 2 x 2 Inverted (Radar)", "type": "PART", "category_id": 116 }, "color_id": 3, "quantity": 2, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "4871", "name": "Slope, Inverted 45 4 x 2 Double with 2 x 2 Cutout", "type": "PART", "category_id": 32 }, "color_id": 3, "quantity": 1, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "30663", "name": "Vehicle, Steering Wheel Small, 2 Studs Diameter", "type": "PART", "category_id": 72 }, "color_id": 86, "quantity": 2, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "43898", "name": "Dish 3 x 3 Inverted (Radar)", "type": "PART", "category_id": 116 }, "color_id": 11, "quantity": 1, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "4697b", "name": "Pneumatic T Piece Second Version (T Bar with Ball in Center)", "type": "PART", "category_id": 159 }, "color_id": 86, "quantity": 4, "extra_quantity": 1, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "44674", "name": "Vehicle, Mudguard 2 x 4 with Headlights Overhang", "type": "PART", "category_id": 77 }, "color_id": 3, "quantity": 2, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "98721", "name": "Minifigure, Weapon Batman Batarang (2 Bat Wings with Bar in Middle)", "type": "PART", "category_id": 19 }, "color_id": 11, "quantity": 4, "extra_quantity": 1, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "61780", "name": "Container, Box 2 x 2 x 2 - Top Opening", "type": "PART", "category_id": 10 }, "color_id": 85, "quantity": 2, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "64644", "name": "Minifigure, Utensil Telescope", "type": "PART", "category_id": 18 }, "color_id": 120, "quantity": 2, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "64728", "name": "Minifigure, Utensil Dynamite Sticks Bundle", "type": "PART", "category_id": 18 }, "color_id": 5, "quantity": 5, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "85861", "name": "Plate, Round 1 x 1 with Open Stud", "type": "PART", "category_id": 28 }, "color_id": 5, "quantity": 4, "extra_quantity": 1, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "92280", "name": "Plate, Modified 1 x 2 with Clip on Top", "type": "PART", "category_id": 27 }, "color_id": 3, "quantity": 2, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "98138pb007", "name": "Tile, Round 1 x 1 with Black Eye with Pupil Pattern", "type": "PART", "category_id": 812 }, "color_id": 1, "quantity": 3, "extra_quantity": 1, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "98100", "name": "Cone 2 x 2 Truncated", "type": "PART", "category_id": 21 }, "color_id": 69, "quantity": 2, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "11476", "name": "Plate, Modified 1 x 2 with Clip on Side (Horizontal Grip)", "type": "PART", "category_id": 27 }, "color_id": 3, "quantity": 2, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "85984pb127", "name": "Slope 30 1 x 2 x 2/3 with Red, White, and Silver Buttons, Dark Red Screens Pattern", "type": "PART", "category_id": 33 }, "color_id": 85, "quantity": 1, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "35464", "name": "Slope 45 1 x 1 Double", "type": "PART", "category_id": 31 }, "color_id": 4, "quantity": 2, "extra_quantity": 1, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "sh647", "name": "The Penguin - Bright Waistcoat", "type": "MINIFIG", "category_id": 768 }, "color_id": 0, "quantity": 1, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "sh650", "name": "Harley Quinn - Jacket Open, Corset", "type": "MINIFIG", "category_id": 768 }, "color_id": 0, "quantity": 1, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "sh689", "name": "Batman - Light Bluish Gray Suit with Yellow Belt, Black Crest, Mask and Cape (Type 3 Cowl)", "type": "MINIFIG", "category_id": 768 }, "color_id": 0, "quantity": 1, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] }, { "match_no": 0, "entries": [ { "item": { "no": "40453-1", "name": "Batman vs. The Penguin & Harley Quinn blister pack", "type": "INSTRUCTION", "category_id": 768 }, "color_id": 0, "quantity": 1, "extra_quantity": 0, "is_alternate": false, "is_counterpart": false } ] } ] }'));
return users;
}
}
}
class _JsonParseDemoState extends State<JsonParseDemo> {
Bricklink? _bricklink;
var _loading = false;
@override
void initState() {
super.initState();
_loading = true;
Services.getUsers().then((bricklink) {
setState(() {
_bricklink = bricklink;
_loading = false;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(_loading ? 'Loading...' : 'BrickLink'),
),
body: Container(
color: Colors.white,
child: ListView.builder(
itemCount: null == _bricklink ? 0 : _bricklink!.length,
itemBuilder: (context, index) {
Bricklink bricklink = _bricklink!;
return ListTile(
title: Text(bricklink.data[index].entries[0].item.no),
subtitle: Text(bricklink.data[index].entries[0].item.name),
leading: CircleAvatar(
child:
Text(bricklink.data[index].entries[0].quantity.toString())),
);
},
),
),
);
}
}
class JsonParseDemo extends StatefulWidget {
//
JsonParseDemo() : super();
@override
_JsonParseDemoState createState() => _JsonParseDemoState();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.