為什么還是不顯示資料。嘗試了多種格式。但它仍然顯示錯誤頁面。我遵循我在 youtube 和本組中觀看和閱讀的所有程式。但我仍然無法加載 json 檔案
class _PlantPageState extends State<PlantPage> {
List<Plants> planty = [];
Future getPlantsLocally() async {
final assetBundle = DefaultAssetBundle.of(context);
final data = await assetBundle.loadString('assets/plants.json');
final body = await json.decode(data);
var list = body["plant"] as List<dynamic>;
setState(() {
planty = list.map((e) => Plants.fromJson(e)).toList();
});
}
@override
Widget build(BuildContext context) => Scaffold(
body: FutureBuilder(
future: getPlantsLocally(),
builder:(context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return Center(child: CircularProgressIndicator());
default:
if (planty.length > 0) {
return Container(
height: 200,
child: ListView.builder(
itemCount: planty.length,
itemBuilder: (BuildContext context, index) {
return Card(
margin: EdgeInsets.all(15.0),
color: Colors.green[600],
child: ListTile(
title: Text(planty[index].name?? 'name',),
),
);
}
),
);
}
else {
return Center(child: Text('Error Error'),);
}
}
這是我的資料庫示例。我已經使用資料類生成器決議它。是我的資料庫不對嗎?還是我的提取檔案?
{
"plant":
[
{
"id": "1",
"name": "ALOCASIA-ELEPHANT EARS",
"image": "assets/images/ALOCASIA.jpeg",
"descript": "Alocasia plant (Alocasia mortfontanensis) is a hybrid species between Alocasia longiloba and Alocasia sanderiana. The Alocasia is known for its large leaves and wide variety of cultivars within the species. Alocasia plant is native to tropical Asia and Australia.",
"charac": [{
"planttype": "Herb",
"lifespan": "Perennial",
"bloomtime": "Spring, summer",
"plantheight": "1-2 feet",
"spread": "7 feet",
"leafColor": "PurpleGreenGreySilver"
}
],
"scienclass": [
{
"genus": "Alocasia - Elephant's-ears, Taro, Kris plant",
"family": " Araceae - Arum, Aroids ",
"order": "Alismatales - Water plantains and allies",
"classes": "Liliopsida - Monocotyledons, Monocots ",
"phylum":"Tracheophyta - Vascular plants, Seed plants, Ferns, Tracheophytes"
}
],
"pestdesease": " Stem rot, crown rot, root rot, leaf spot, mealy bugs, aphids",
"requirements":[{
"difficultyrating": "Alocasia plant is super easy to take care of, with resistance to almost all pests and diseases. It is a perfect option for gardeners with brown thumbs.",
"sunlight": "Full shade to partial sun",
"hardenesszone": " 9-11 ",
"soil": "Loose, fertile and well-drained humus soil"
}
],
"careguide":[
{
"water": "Moisture-loving, keep the soil moist but do not let water accumulate.",
"fertilizaton": "Fertilization once in spring. ",
"pruning": "Fertilization once in spring. ",
"plantingtime": "Spring, summer, autumn ",
"propagation": "Division ",
"pottingsuggestion": " Needs excellent drainage in pots."
}
],
"toxictohuman": "Is the alosdadadsadadsa",
},
你認為伙計們這個流程的錯誤是什么?
uj5u.com熱心網友回復:
您是否嘗試查看 json 檔案的名稱和檔案的路徑,以確保與您嘗試訪問的檔案匹配,并確保在 pubspec.yaml 檔案中參考它
uj5u.com熱心網友回復:
希望這有效。
class _PlantPageState extends State<PlantPage> {
Future<List> getPlantsLocally() async {
final assetBundle = DefaultAssetBundle.of(context);
final data = await assetBundle.loadString('assets/plants.json');
final body = await json.decode(data);
var list = body["plant"] as List<dynamic>;
var planty = list.map((e) => Plants.fromJson(e)).toList();
return planty;
}
@override
Widget build(BuildContext context) => Scaffold(
body: FutureBuilder(
future: getPlantsLocally(),
builder:(context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return Center(child: CircularProgressIndicator());
default:
if (snapshot.data.length > 0) {
return Container(
height: 200,
child: ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, index) {
return Card(
margin: EdgeInsets.all(15.0),
color: Colors.green[600],
child: ListTile(
title: Text(snapshot.data[index].name?? 'name',),
),
);
}
),
);
}
else {
return Center(child: Text('Error Error'),);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/342571.html
上一篇:C#服務器端管理UTC日期
下一篇:顫動圓形滑塊
