I am trying to restore pages stack after a browser refresh button pressing.
Using onGenerateRoute
on MaterialApp
I can restore the single page, even with arguments using SharedPrefences
api and Firestore data but when I press the browser back button it goes to the home page.
How can I save the flutter page stack or at least only the branch of the current page in the stack?
This is an example of onGenerateRoute
implementation:
if (settings.name == ProductEditScreen.routeName) {
final args = settings.arguments != null
? settings.arguments as ProductEditScreenArguments
: null;
return MaterialPageRoute(
settings: RouteSettings(name: ProductEditScreen.routeName),
builder: (context) {
return settings.arguments != null
? ProductEditScreen(
arguments: ProductEditScreenArguments(
args!.product,
args.store,
args.locations,
),
setCurrentPageData: () async {
SharedPreferences prefs =
await SharedPreferences.getInstance();
//prefs.setString("sessionToken", user!.sessionToken!);
prefs.setString("productId", args.product.id!);
prefs.setString("storeId", args.store.id!);
},
)
: FutureBuilder<CurrentPageData>(future: () async {
SharedPreferences prefs =
await SharedPreferences.getInstance();
prefs.getString("sessionToken");
String? storeId = prefs.getString("storeId");
String? productId = prefs.getString("productId");
List<Location> locations = await EntityControllers
.locationController
.readAll()
.then((locations) => locations.cast<Location>());
Store store = (await EntityControllers.storeController
.readOneByAttribute("id", storeId!)) as Store;
Product product =
await ProductController.productQueryController(
productId!);
return CurrentPageData(
// sessionToken: user!.sessionToken!,
screenName: ProductEditScreen.routeName,
data: {
"store": store.toJson(),
},
dataEntity: {"product": product},
dataList: locations.map((e) => e.toJson()).toList(),
);
}(), builder: (BuildContext context,
AsyncSnapshot<CurrentPageData> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
}
if (snapshot.hasData) {
Store store =
Store.fromJson(snapshot.data!.data!["store"]!);
List<Location> locations = snapshot.data!.dataList!
.map((location) => Location.fromJson(location))
.toList();
Product product =
snapshot.data!.dataEntity!["product"] as Product;
return ProductEditScreen(
arguments: ProductEditScreenArguments(
product,
store,
locations,
),
setCurrentPageData: () async {
SharedPreferences prefs =
await SharedPreferences.getInstance();
//prefs.setString("sessionToken", user!.sessionToken!);
print("screen session token:");
prefs.setString("storeId", store.id!);
prefs.setString("productId", product.id!);
},
);
} else {
if (snapshot.hasError) {
print(snapshot.error);
}
return DashboardScreen();
}
});
},
);
}
Before ProductEditScreen
page there is ProductListScreen
page.
I need to put them into the stack.