簡體   English   中英

記事本 - RangeError(索引):無效值:有效值范圍為空:0 - flutter

[英]Notepad - RangeError (index): Invalid value: Valid value range is empty: 0 - flutter

我想將文本和日期保存到 txt 文件並加載它,但我收到“RangeError (index): Invalid value: Valid value range is empty: 0”

void _loadNotes() async {
    var directory = await getApplicationDocumentsDirectory();
    var file = File("${directory.path}/notable.txt");
    if (!await file.exists()) {
      file.createSync();
      file.writeAsStringSync("Hello, World!#This is your first note.\n");
    }
    var contents = await file.readAsString();
    var contentsList = contents.split("\n");
    for (var i = 0; i < contentsList.length; i++) {
      var noteLine = contentsList[i].split("#");
      _notes.add(noteLine[0]);
      _noteAddedDates.add(noteLine[1]);
    }
  }
  void _saveNotes() async {
    var directory = await getApplicationDocumentsDirectory();
    var file = File("${directory.path}/notable.txt");
    var contents = "";
    for (var i = 0; i < _notes.length; i++) {
      contents += _notes[i] + "#" + _noteAddedDates[i] + "\n";
    }
    await file.writeAsString(contents);
  }
    void _loadNotes() async {
        var directory = await getApplicationDocumentsDirectory();
        var file = File("${directory.path}/notable.txt");
        if (!await file.exists()) {
          file.createSync();
          file.writeAsStringSync("Hello, World!#This is your first note.\n");
        }
        var contents = await file.readAsString();
        var contentsList = contents.split("\n");
    Future.delayed(Duration(milliseconds: 1000), () {

//add this delay 
          for (var i = 0; i < contentsList.length; i++) {
          var noteLine = contentsList[i].split("#");
          _notes.add(noteLine[0]);
          _noteAddedDates.add(noteLine[1]);
        }                                            
            });
     
      }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM