From b35d5f4337d103dc6621acb5ab2ce28db5990909 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Sun, 29 Dec 2019 12:03:42 +0100 Subject: [PATCH] Use YAML serializer package This code might be useful to someone else --- lib/core/note.dart | 2 +- lib/core/serializers.dart | 19 +++++-------------- pubspec.lock | 7 +++++++ pubspec.yaml | 1 + 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/lib/core/note.dart b/lib/core/note.dart index 1955c011..f9403e91 100644 --- a/lib/core/note.dart +++ b/lib/core/note.dart @@ -101,7 +101,7 @@ class Note with ChangeNotifier implements Comparable { if (_title.isEmpty) { _data.props.remove('title'); } else { - _data.props['title'] = "'" + title.replaceAll("'", "''") + "'"; + _data.props['title'] = title; } notifyListeners(); } diff --git a/lib/core/serializers.dart b/lib/core/serializers.dart index 7d8aee4f..088f271f 100644 --- a/lib/core/serializers.dart +++ b/lib/core/serializers.dart @@ -2,6 +2,7 @@ import 'dart:collection'; import 'package:fimber/fimber.dart'; import 'package:yaml/yaml.dart'; +import 'package:yaml_serializer/yaml_serializer.dart'; class NoteData { String body = ""; @@ -120,25 +121,15 @@ class MarkdownYAMLSerializer implements NoteSerializer { return note.body; } - const serparator = '---\n'; - var str = ""; - str += serparator; - - str += toYAML(note.props); - str += serparator; + var str = toYamlHeader(note.props); str += '\n'; str += note.body; return str; } - static String toYAML(Map map) { - var str = ""; - - map.forEach((key, value) { - String val = value.toString(); - str += key + ": " + val + "\n"; - }); - return str; + static String toYamlHeader(Map data) { + var yaml = toYAML(data); + return "---\n" + yaml + "---\n"; } } diff --git a/pubspec.lock b/pubspec.lock index f3a5f19a..89d104b9 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -745,6 +745,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.2.0" + yaml_serializer: + dependency: "direct main" + description: + name: yaml_serializer + url: "https://pub.dartlang.org" + source: hosted + version: "0.0.2" sdks: dart: ">=2.5.0 <3.0.0" flutter: ">=1.12.13+hotfix.4 <2.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index c48f0591..06f04cfe 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -33,6 +33,7 @@ dependencies: git_bindings: ^0.0.6 fetch_app_logs: ^0.0.2 flutter_runtime_env: ^0.0.1 + yaml_serializer: ^0.0.2 connectivity: ^0.4.6+1 dev_dependencies: