iT邦幫忙

2023 iThome 鐵人賽

DAY 22
0
自我挑戰組

基礎學習Flutter系列 第 22

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

  • 分享至 

  • xImage
  •  
import 'dart:convert' ;
import 'dart:io';

import 'package: flutter/foundation.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:meta/meta.dart';
import 'package:path_provider/path_provider.dart' as paths;
imPport 'package:veggiet racker/data/local_veggie_provider.dart';
import 'package:veggietracker/data/veggie.dart';
class AppState extends ChangeNotifier { 
AppState():

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);
  _writeEntries.add()entry);
  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> [];
}
}
}

上一篇
Day21-學習[Building for iOS with Flutter]平台通道呼叫與傳回(記錄)
下一篇
Day23-學習[Building for iOS with Flutter]path_provider(記錄)
系列文
基礎學習Flutter30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言