[英]Flutter - ListView.builder() not working after the JSON fetch
[英]convert JSON to ListView.builder in flutter
我是編程新手,所以請耐心等待我,我正在嘗試將此 JSON 轉換為像這種設計一樣顯示,我已經完成了 JSON 解碼,但是對於示例 JSON,所以我了解它是如何工作的,但是對於復雜的我來說困惑所以我真的很感激如果有人幫助
JSON 代碼:
[
{
"Category": "Gro",
"stores": [
{
"name": "market",
"logo_url": "https://www.google.com/signpost-150x150.png",
"phone_1": "1111111111",
"phone_2": "1111111111",
"location": "https://maps.google.com/location"
},
{
"name": "mall",
"logo_url": "https://www.google.com/signpost-150x150.png",
"phone_1": "1111111111",
"phone_2": "1111111111",
"location": "https://maps.google.com/location"
}
]
},
{
"Category": "Food",
"stores": [
{
"name": "Food Time",
"logo_url": "https://www.google.com/signpost-150x150.png",
"phone_1": "1111111111",
"phone_2": "1111111111",
"location": "https://maps.google.com/location"
},
{
"name": "let's eat",
"logo_url": "https://www.google.com/signpost-150x150.png",
"phone_1": "1111111111",
"phone_2": "1111111111",
"location": "https://maps.google.com/location"
}
]
},
{
"Category": "Personal Care",
"stores": [
{
"name": "Body",
"logo_url": "https://www.google.com/signpost-150x150.png",
"phone_1": "1111111111",
"phone_2": "1111111111",
"location": "https://maps.google.com/location"
},
{
"name": "Hair",
"logo_url": "https://www.google.com/signpost-150x150.png",
"phone_1": "1111111111",
"phone_2": "1111111111",
"location": "https://maps.google.com/location"
}
]
}
]
##############################################
##############################################
這是我想出的
import 'package:side_menu.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:http/http.dart';
import 'dart:convert';
Future<List> shopsList() async {
Response response = await get('JSON URL');
if (response.statusCode == 200) {
var shopsData = jsonDecode(response.body);
return shopsData;
} else {
print(response.statusCode);
}
}
class ShowShops extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
drawer: SideMenu(),
appBar: AppBar(),
body: Column(
children: [
Expanded(
child: Container(
child: FutureBuilder(
future: shopsList(),
builder: (context, shops) {
if (shops.hasData) {
return ListView.builder(
itemCount: shops.data.length,
itemBuilder: (BuildContext context, int index) {
Map shopInfo = shops.data[index];
String cat = shopInfo[index]['Category'];
return Card(
child: Text(cat),
);
},
);
}
return Center(
child: SpinKitWave(
color: Color(0xff023246),
size: 100,
),
//CircularProgressIndicator(),
);
}),
),
)
],
),
);
}
}
但是我收到錯誤,我很困惑如何在類別下顯示商店
提前感謝您的幫助
添加,
第 9 行
"phone_2": "1111111111"
試試我的代碼
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: Test(),
);
}
}
class Test extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: FutureBuilder(
future: http.get('https://dc-apps.net/map/services.json'),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
}
List data = json.decode(snapshot.data.body);
// print(data);
List categoriesnames = [];
List stores = [];
data.forEach((element) {
categoriesnames.add(element["Category"]);
stores.add(element['stores']);
});
// return Text('see');
return ListView.builder(
itemCount: data.length,
itemBuilder: (context, index) {
// print(stores[index][index]['name']);
return CardItem(
categoryname: categoriesnames[index],
sotories: stores[index],
);
},
);
},
),
);
}
}
class CardItem extends StatelessWidget {
final String categoryname;
List sotories;
CardItem({this.categoryname, this.sotories});
@override
Widget build(BuildContext context) {
return Container(
child: Expanded(
child: Column(
children: [
Text(categoryname),
SizedBox(
height: 5,
),
ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: sotories.length,
itemBuilder: (context, index) => ListTile(
title: Text(sotories[index]['name']),
subtitle: Column(
children: [
Text(sotories[index]['phone_1']),
Text(sotories[index]['phone_2']),
],
),
trailing: CircleAvatar(
radius: 50,
backgroundImage: NetworkImage(sotories[index]['logo_url']),
),
),
),
],
),
),
);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.