首页 > 解决方案 > 如何更新屏幕上的目录文件?

问题描述

我有四个来自目录的列表。我遇到的问题是,当这些目录被修改(在删除和添加的情况下)时,屏幕没有被修改(使用我从列表中使用的数据)。系统仅在我再次运行该应用程序时才检测到此修改(即使在它检测到的热加载中也没有)。

我已经阅读了寻找解决方案的参考资料(https://api.flutter.dev/flutter/dart-io/Directory-class.html),但我没有找到任何可以帮助的东西。有没有人遇到过类似的问题并设法解决了?

class DashboardBiologia extends StatefulWidget {
  @override
  _DashboardBiologiaState createState() => _DashboardBiologiaState();
}

class _DashboardBiologiaState extends State<DashboardBiologia> {

  List resultados = [];
  List ano1 = [];
  List ano2 = [];
  List ano3 = [];

  @override
  void initState() {
    super.initState();
    _readDataresultados().then((data) {
      setState(() {
        if(data.isNotEmpty){
          resultados = jsonDecode(data);
        }
      });
    });
    _readDataano1().then((data) {
      setState(() {
        if(data.isNotEmpty){
          ano1 = jsonDecode(data);
        }
      });
    });
    _readDataano2().then((data) {
      setState(() {
        if(data.isNotEmpty){
          ano2 = jsonDecode(data);
        }
      });
    });
    _readDataano3().then((data) {
      setState(() {
        if(data.isNotEmpty){
          ano3 = jsonDecode(data);
        }
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return DashBoard(
      primarycolor: Color.fromARGB(250, 19, 203, 147),
      secondarycolor: Color.fromARGB(40, 19, 203, 147),
      materia: "Biologia",
      percent1:(ano1.length == 0) ? 0: (((ano1.where((element) => element["check"] == true)).length) / (ano1.length)),
      percent2: (ano2.length == 0) ? 0: (((ano2.where((element) => element["check"] == true)).length) / (ano2.length)),
      percent3:(ano3.length == 0) ? 0: (((ano3.where((element) => element["check"] == true)).length) / (ano3.length)),
      variant: (resultados.length == 0 || resultados.length < 2) ? 0 :(((resultados[resultados.length-1]["rightquestions"]/resultados[resultados.length-1]["allquestions"])*100.round()).roundToDouble()-
          ((resultados[resultados.length-2]["rightquestions"]/resultados[resultados.length-2]["allquestions"])*100.round()).roundToDouble()),
      rendimento: resultados.length == 0 ? 0
          : ((resultados.map((resultados) => resultados["rightquestions"]).toList().reduce((curr, next) => curr + next)/resultados.map((resultados) => resultados["allquestions"]).toList().reduce((curr, next) => curr + next))*100).round(),
      x1: (resultados.length == 0 || resultados.length < 10) ? 0 :((resultados[resultados.length-10]["rightquestions"]/resultados[resultados.length-10]["allquestions"])*100.round()).roundToDouble(),
      x2: (resultados.length == 0 || resultados.length < 9) ? 0 :((resultados[resultados.length-9]["rightquestions"]/resultados[resultados.length-9]["allquestions"])*100.round()).roundToDouble(),
      x3: (resultados.length == 0 || resultados.length < 8) ? 0 :((resultados[resultados.length-8]["rightquestions"]/resultados[resultados.length-8]["allquestions"])*100.round()).roundToDouble(),
      x4: (resultados.length == 0 || resultados.length < 7) ? 0 :((resultados[resultados.length-7]["rightquestions"]/resultados[resultados.length-7]["allquestions"])*100.round()).roundToDouble(),
      x5: (resultados.length == 0 || resultados.length < 6) ? 0 :((resultados[resultados.length-6]["rightquestions"]/resultados[resultados.length-6]["allquestions"])*100.round()).roundToDouble(),
      x6: (resultados.length == 0 || resultados.length < 5) ? 0 :((resultados[resultados.length-5]["rightquestions"]/resultados[resultados.length-5]["allquestions"])*100.round()).roundToDouble(),
      x7: (resultados.length == 0 || resultados.length < 4) ? 0 :((resultados[resultados.length-4]["rightquestions"]/resultados[resultados.length-4]["allquestions"])*100.round()).roundToDouble(),
      x8: (resultados.length == 0 || resultados.length < 3) ? 0 :((resultados[resultados.length-3]["rightquestions"]/resultados[resultados.length-3]["allquestions"])*100.round()).roundToDouble(),
      x9: (resultados.length == 0 || resultados.length < 2) ? 0 :((resultados[resultados.length-2]["rightquestions"]/resultados[resultados.length-2]["allquestions"])*100.round()).roundToDouble(),
      x10:(resultados.length == 0 || resultados.length < 1) ? 0 :((resultados[resultados.length-1]["rightquestions"]/resultados[resultados.length-1]["allquestions"])*100.round()).roundToDouble(),
    );
  }
  Future<File> _getFileresultados() async {
    final directory = await getApplicationDocumentsDirectory();
    final directoryfoldertest = File("${directory.path}/materiasbiologia/materiasbiologia.json");

    if (await directoryfoldertest.exists()) {
      return directoryfoldertest.absolute;
    } else {
      final File directoryfoldertest2 = await directoryfoldertest.create(
          recursive: true);
      return directoryfoldertest2.absolute;
    }
  }

  Future<String> _readDataresultados() async {
    try {
      final file = await _getFileresultados();

      return file.readAsString();
    } catch (e) {
      return null;
    }
  }

  Future<File> _getFileano1() async {
    final directory = await getApplicationDocumentsDirectory();
    final directoryfoldertest = File("${directory.path}/primeiroanobiologia/primeiroanobiologia.json");

    if (await directoryfoldertest.exists()) {
      return directoryfoldertest.absolute;
    } else {
      final File directoryfoldertest2 = await directoryfoldertest.create(
          recursive: true);
      return directoryfoldertest2.absolute;
    }
  }
  Future<String> _readDataano1() async {
    try {
      final file = await _getFileano1();

      return file.readAsString();
    } catch (e) {
      return null;
    }
  }

  Future<File> _getFileano2() async {
    final directory = await getApplicationDocumentsDirectory();
    final directoryfoldertest = File("${directory.path}/segundoanobiologia/segundoanobiologia.json");

    if (await directoryfoldertest.exists()) {
      return directoryfoldertest.absolute;
    } else {
      final File directoryfoldertest2 = await directoryfoldertest.create(
          recursive: true);
      return directoryfoldertest2.absolute;
    }
  }
  Future<String> _readDataano2() async {
    try {
      final file = await _getFileano2();

      return file.readAsString();
    } catch (e) {
      return null;
    }
  }

  Future<File> _getFileano3() async {
    final directory = await getApplicationDocumentsDirectory();
    final directoryfoldertest = File("${directory.path}/terceiroanobiologia/terceiroanobiologia.json");

    if (await directoryfoldertest.exists()) {
      return directoryfoldertest.absolute;
    } else {
      final File directoryfoldertest2 = await directoryfoldertest.create(
          recursive: true);
      return directoryfoldertest2.absolute;
    }
  }
  Future<String> _readDataano3() async {
    try {
      final file = await _getFileano3();

      return file.readAsString();
    } catch (e) {
      return null;
    }
  }
}

标签: flutterdart

解决方案


尝试setState在更改目录中的文件时调用。但是setState不会再次调用initState,因此您应该直接重新加载目录或使用build方法加载文件夹来更改应用程序功能。


推荐阅读