【发布时间】:2021-10-21 18:45:30
【问题描述】:
为什么它告诉我不正确地使用 ParentDataWidget?我是否正确加载了我的 json 数据?我的目标是将我的 json 数据放入带有搜索栏的 ListView 中。加载我的数据时结构是否正确?实现我的搜索栏时它会起作用吗?我尝试过使用 FutureBuilder 的其他实现,但没有成功
尝试加载资产时出错:无法在“assets/assets/files/myfiles.json”加载资产 (404)
class MainPage3 extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<MainPage3> {
List _items = [];
List _meanings = [];
List _examples = [];
// Fetch content from the json file
Future<void> readJson() async {
final String response = await rootBundle.loadString('assets/files/myfiles.json');
final data = await json.decode(response);
setState(() {
_items = data["VERB"];
_meanings = data['MEANING'];
_examples = data['EXAMPLE'];
});
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text(
'Kindacode.com',
),
),
body: Padding(
padding: const EdgeInsets.all(25),
child: Column(
children: [
ElevatedButton(
child: Text('Load Data'),
onPressed: readJson,
),
// Display the data loaded from sample.json
_items.length > 0
? Expanded(
child: ListView.builder(
itemCount: _items.length,
itemBuilder: (context, index) {
return Card(
margin: EdgeInsets.all(10),
child: ListTile(
leading: Text(_items[index]["VERB"]),
title: Text(_meanings[index]["MEANING"]),
subtitle: Text(_examples[index]["EXAMPLE"]),
),
);
},
),
)
: Container()
],
),
),
);
}
}
我的json数据是这样的:
{"VERB": "ASK OUT", "MEANING": "To invite on a date", "EXAMPLE": "Martin invited Carol out to dinner on Sunday"},
【问题讨论】: