Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 尝试从JSON文件的最后一个条目检索数据时出错_Android_Json_Flutter_Runtime Error - Fatal编程技术网

Android 尝试从JSON文件的最后一个条目检索数据时出错

Android 尝试从JSON文件的最后一个条目检索数据时出错,android,json,flutter,runtime-error,Android,Json,Flutter,Runtime Error,我正在尝试读取一个JSON文件,并从最后一个条目中获取一个值,以便在构建小部件时显示在屏幕上。JSON文件存储在本地,并已添加到pubspec.yaml中。每次我进入“我的测试”页面查看是否显示该值时,我都会看到下面的错误屏幕截图。我不知道我做错了什么 这是我的波多: import 'dart:convert'; List<HistoryData> historyDataFromJson(String str) => List<HistoryData>.from

我正在尝试读取一个JSON文件,并从最后一个条目中获取一个值,以便在构建小部件时显示在屏幕上。JSON文件存储在本地,并已添加到pubspec.yaml中。每次我进入“我的测试”页面查看是否显示该值时,我都会看到下面的错误屏幕截图。我不知道我做错了什么

这是我的波多:

import 'dart:convert';

List<HistoryData> historyDataFromJson(String str) => List<HistoryData>.from(json.decode(str).map((x) => HistoryData.fromJson(x)));

String historyDataToJson(List<HistoryData> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class HistoryData {
  HistoryData({
      this.date,
      this.weight,
      this.loss,
      this.change,
  });

  String date;
  String weight;
  String loss;
  String change;

  factory HistoryData.fromJson(Map<String, dynamic> json) => HistoryData(
      date: json["date"],
      weight: json["weight"],
      loss: json["loss"],
      change: json["change"],
  );

  Map<String, dynamic> toJson() => {
      "date": date,
      "weight": weight,
      "loss": loss,
      "change": change,
  };
}

rootBundle.loadString()
返回一个
Future
,如错误所示。然后您正在对它执行
toString
,这是…的
实例,导致您的特定错误,因为它不是JSON

您需要
等待
rootBundle.loadString('json_files/history.json')的结果

Future getCurrentWeight()异步{
List historyList=historyDataFromJson(等待rootBundle.loadString('json_files/history.json');
变量历史=historyList[historyList.length-1];
字符串当前值=history.weight;
回流;
}
然后,您必须修改小部件,以便使用
FutureBuilder
正确处理显示这些未来数据

class\u TestState扩展状态{
未来电流;
@凌驾
void initState(){
super.initState();
current=getCurrentWeight();//获取您的未来
}
@凌驾
小部件构建(构建上下文){
回归未来建设者(
future:current,//传递未来
生成器:(上下文,快照){
if(snapshot.hasData){//仅在数据可用时显示数据
返回容器(
儿童:中心(
child:Text(snapshot.data)//在此处获取数据
),
);
}
return CircularProgressIndicator();//否则显示
}
);
}
未来getCurrentWeight()异步{
List historyList=historyDataFromJson(等待rootBundle.loadString('json_files/history.json');
变量历史=historyList[historyList.length-1];
字符串当前值=history.weight;
回流;
}
}

您还可以向我们展示示例JSON数据吗?我已经在JSON文件中添加了一些数据。您可以发布完整的JSON文件吗?看起来JSON数据有问题,有点不完整或无效字符我已经用我使用的整个JSON文件更新了帖子。这不应该有什么问题,因为我已经成功地在另一个屏幕上创建了一个ListView。你抢先找到了答案!无论如何,谢谢你,我感谢你对错误的解释。我找到了这篇有答案的帖子,它为任何觉得有用的人提供了更多的细节:。
class Test extends StatefulWidget {
  @override
  _TestState createState() => _TestState();
}

class _TestState extends State<Test> {
  String current = '';

  void initState() {
    super.initState();
    current = getCurrentWeight();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: Text(current)
      ),
    );
  }

  String getCurrentWeight() {
    List<HistoryData> historyList = historyDataFromJson(rootBundle.loadString('json_files/history.json').toString());
    var history = historyList[historyList.length-1];
    String current = history.weight;
    return current;
  }
}
[
    {
        "date" : "17/06/2020",
        "weight" : "95.0",
        "loss" : "+0.0",
        "change" : "+0.0"
    },
    {
        "date" : "18/06/2020",
        "weight" : "96.0",
        "loss" : "+1.0",
        "change" : "+1.1"
    },
    {
        "date" : "19/06/2020",
        "weight" : "95.1",
        "loss" : "-0.9",
        "change" : "-0.9"
    },
    {
        "date" : "20/06/2020",
        "weight" : "94.2",
        "loss" : "-0.9",
        "change" : "-0.9"
    },
    {
        "date" : "21/06/2020",
        "weight" : "92.0",
        "loss" : "-2.2",
        "change" : "-2.3"
    },
    {
        "date" : "22/06/2020",
        "weight" : "90.6",
        "loss" : "-1.4",
        "change" : "-1.5"
    },
    {
        "date" : "23/06/2020",
        "weight" : "89.6",
        "loss" : "-1.0",
        "change" : "-1.1"
    },
    {
        "date" : "24/06/2020",
        "weight" : "89.4",
        "loss" : "-0.2",
        "change" : "-0.2"
    },
    {
        "date" : "25/06/2020",
        "weight" : "87.8",
        "loss" : "-1.6",
        "change" : "-1.8"
    },
    {
        "date" : "26/06/2020",
        "weight" : "86.1",
        "loss" : "-1.7",
        "change" : "-1.9"
    }
]