首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何更新屏幕上的目录文件?

如何更新屏幕上的目录文件?
EN

Stack Overflow用户
提问于 2021-01-30 01:41:13
回答 1查看 22关注 0票数 0

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

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

代码语言:javascript
复制
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;
    }
  }
}
EN

回答 1

Stack Overflow用户

发布于 2021-01-30 02:50:17

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65959278

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档