Flutter/外部ライブラリ/pref and json
提供: 初心者エンジニアの簡易メモ
prefとjsonを使ったサンプル
prefとjsonを使ったdaoクラスを作る
lib/model/pref.dart
import 'dart:convert'; import 'package:memotyou/home/init/article.dart'; import 'package:shared_preferences/shared_preferences.dart'; class Pref { static String articlesKey = 'articles'; static String articleIdKey = 'articleId'; static String articleBodyKey = 'articleBody'; static void saveArticles(List<Article> articles) async { SharedPreferences prefs = await SharedPreferences.getInstance(); List<String> prefArticles = new List<String>(); articles.forEach((article) { String articleJson = json.encode(article.toJson()); print("json save $articleJson"); prefArticles.add(articleJson); }); await prefs.setStringList(articlesKey, prefArticles); } static Future<List<Article>> getArticles() async { SharedPreferences prefs = await SharedPreferences.getInstance(); List<String> prefArticles = prefs.getStringList(articlesKey); if (prefArticles == null) { prefArticles = new List<String>(); } List<Article> articles = List<Article>(); prefArticles.forEach((articleJson) { print("json load $articleJson"); dynamic mapArticle = json.decode(articleJson); Article article = Article.fromJson(mapArticle); articles.add(article); }); return articles; } }
lib/dao/article_dao.dart
import 'package:collection/collection.dart'; import 'package:memotyou/home/init/article.dart'; import 'package:memotyou/home/model/pref.dart'; class ArticleDao { Future<List<Article>> findAll() async { List<Article> tmpArticles = new List<Article>(); List<Article> articles = await Pref.getArticles(); articles.forEach((tmpArticle) { if (tmpArticle != null && tmpArticle.id != null) { tmpArticles.add(tmpArticle); } }); reverse(tmpArticles); return tmpArticles; } Future<Article> findRowById(int id) async { List<Article> articles = await Pref.getArticles(); Article article; articles.forEach((tmpArticle) { print(tmpArticle.name); print(tmpArticle.body); if (tmpArticle.id == id) { article = tmpArticle; } }); return article; } Future<int> insert(Article article) async { List<Article> articles = await Pref.getArticles(); List<Article> tmpArticles = new List<Article>(); int maxId = 0; articles.forEach((tmpArticle) { if (tmpArticle != null && tmpArticle.id != null) { if (tmpArticle.id > maxId) { maxId = tmpArticle.id; } tmpArticles.add(tmpArticle); } }); article.id = maxId + 1; tmpArticles.add(article); int lastId = article.id; Pref.saveArticles(tmpArticles); return lastId; } Future<int> updateById(Article article,int id) async { List<Article> articles = await Pref.getArticles(); bool updateFlug = false; List<Article> tmpArticles = new List<Article>(); int lastId = 0; articles.forEach((tmpArticle) { print(tmpArticle.name); print(tmpArticle.body); if (tmpArticle.id == id) { tmpArticles.add(article); updateFlug = true; lastId = tmpArticle.id; } else { tmpArticles.add(tmpArticle); } }); if (!updateFlug) { tmpArticles.add(article); } Pref.saveArticles(tmpArticles); return lastId; } Future<int> deleteById(int id) async { List<Article> articles = await Pref.getArticles(); List<Article> tmpArticles = new List<Article>(); int lastId = 0; articles.forEach((tmpArticle) { if (tmpArticle.id == id) { lastId = tmpArticle.id; } else { tmpArticles.add(tmpArticle); } }); Pref.saveArticles(tmpArticles); return lastId; } }
main.dart
class _ListPageState extends State<ListPage> { List<Article> articles = new List<Article>(); @override void initState() async { articles.clear(); ArticleDao dao = new ArticleDao(); List<Article> tmpArticles = await dao.findAll(); setState(() { articles = tmpArticles; }); }
↓のようにも書けるがthenは古い書き方なので↑のほうが良いっぽい。
main.dart
class _ListPageState extends State<ListPage> { List<Article> articles = new List<Article>(); @override void initState() { articles.clear(); ArticleDao dao = new ArticleDao(); dao.findAll().then((tmpArticles) { setState(() { articles = tmpArticles; }); }); }
非同期で取得した値をarticlesへ入れる部分についてsetStateで囲むのが味噌です。