From 47a373c13558c76874c97ea8c96a1c7dc4b59072 Mon Sep 17 00:00:00 2001 From: salim lachdhaf Date: Mon, 14 Dec 2020 14:17:28 +0100 Subject: [PATCH] v0.1.1 fix issue ios building --- example/lib/main.dart | 10 ++++++---- ios/Classes/StateStorage.swift | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 ios/Classes/StateStorage.swift diff --git a/example/lib/main.dart b/example/lib/main.dart index e83733d..f164b6e 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -29,17 +29,19 @@ class MyApp extends StatelessWidget { //2- second step: setUp navigation observer navigatorObservers: [SavedLastStateData.instance.navigationObserver], routes: { + HomePage.route: (context) => HomePage(), DummyPage.route: (context) => DummyPage(), }, // restore the route or default to the home page //3- third step: set initial route - initialRoute: SavedLastStateData.instance.lastRoute ?? "/", - home: HomePage(), + initialRoute: SavedLastStateData.instance.lastRoute ?? HomePage.route, ); } } class HomePage extends StatefulWidget { + static const route = 'home'; + @override _HomePageState createState() => _HomePageState(); } @@ -96,7 +98,7 @@ class _HomePageState extends State with LastStateRestoration { ), RaisedButton( child: Text("Go next page"), - onPressed: () => Navigator.of(context).pushNamed("/intermediate"), + onPressed: () => Navigator.of(context).pushNamed(DummyPage.route), ), ], ), @@ -106,7 +108,7 @@ class _HomePageState extends State with LastStateRestoration { } class DummyPage extends StatelessWidget { - static const route = "/intermediate"; + static const route = 'intermediate'; @override Widget build(BuildContext context) { diff --git a/ios/Classes/StateStorage.swift b/ios/Classes/StateStorage.swift new file mode 100644 index 0000000..f36ca36 --- /dev/null +++ b/ios/Classes/StateStorage.swift @@ -0,0 +1,31 @@ +// +// StateStorage.swift +// flutter_last_state +// +// +import Foundation + +public class StateStorage { + public static let instance = StateStorage() + + private var state: [String:Any] = [:] + + func update(state: [String: Any]) { + self.state = state; + } + + func get() -> [String: Any] { + return self.state + } + + public func save(coder: NSCoder) { + NSDictionary(dictionary: ["flutter_state" : state]).encode(with: coder) + } + + public func restore(coder: NSCoder) { + self.state = NSDictionary(coder: coder)?.value(forKey: "flutter_state") as? [String : Any] ?? [:] + } + + private init() { + } +} \ No newline at end of file