我正在尝试读取一个mood.json名为“data”的 json 文件并将其解析为一个名为“data”的列表,但是当我运行时setState(),data从未改变,对这个问题有任何帮助吗?代码如下所示:
class DisplayPage extends StatefulWidget {
@override
_DisplayPageState createState() => new _DisplayPageState();
}
class _DisplayPageState extends State<DisplayPage> {
List _data = [];
Directory dir;
File jsonFile;
String jsonPath;
Future getData() async {
dir = await getApplicationDocumentsDirectory();
jsonPath = dir.path + "/" + "mood.json";
jsonFile = new File(jsonPath);
setState(() {
_data = json.decode(jsonFile.readAsStringSync());
});
}
@override
void initState() {
super.initState();
getData();
print(_data.length);
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new …Run Code Online (Sandbox Code Playgroud)