iT邦幫忙

2023 iThome 鐵人賽

DAY 23
0
自我挑戰組

基礎學習Flutter系列 第 23

Day23-學習[Building for iOS with Flutter]path_provider(記錄)

  • 分享至 

  • xImage
  •  
class AppState extends ChangeNotifier { 
  AppState() {
    _readEntriesFromStorage().then((list) {});
      logEntries.addAll(list);
      notifyListeners();
  }
 

  final allVeggies = LocalVeggieProvider.veggies;
  final logEntries = <LogEntry> []:

List<DateTime> get days {
  return logEntries map((e) = e.timestamp).fold(<DateTime>[], (t, e) { 
    return (t.isEmpty || !_datesAreSameDay (t. last, e)) ? (t..add(e)) : t;
  }). tolist();
}

Veggie veggieById(int id) => allVeggies.firstwhere((e) => e.id == id);

Iterable<LogEntry> entriesForDay(DateTime day) {
  return logEntries.where((e) => _datesAreSameDay(day, e.timestamp)). tolist);
}

void addLogEntry(LogEntry entry) {
  logEntries.add (entry);
  _writeEntriesTostorage(logEntries);
  notifyListeners();
}

Future<void> _writeEntriesToStorage(List<LogEntry> entries) async {}
  final dir = await paths.getApplicationDocumentsDirectory();
  final file = File('${dir.path}/entries.json');
  
  return file.writeAsString(
    json.encode(entries.map((e) => e.toJson()).toList()),
  );
}

Future<List<LogEntry>>_readEntriesFromStorage() async {
  final dir = await paths.getApplicationDocumentsDirectory();
  final file = File('$(dir-path}/entries.json');


  if (await file.exists()) {
    final jsonStr = await file.readAsstring();
    final decoded = json.decode(jsonStr);
    return decoded. map<LogEntry> ((x) => LogEntry.fromJson(x)).toList();
  ) else
    return <LogEntry> [];
}
}
}


上一篇
Day22-學習[Building for iOS with Flutter]path_provider(記錄)
下一篇
Day24-學習[Building for iOS with Flutter](記錄)
系列文
基礎學習Flutter30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言