unstory 0.1.1
unstory: ^0.1.1 copied to clipboard
A browser-style history abstraction for Dart and Flutter routing.
unstory #
A small, cross-platform history abstraction for Dart and Flutter routing.
Unstory provides a minimal, browser-like History interface with consistent
semantics and multiple implementations (memory, browser path, and hash).
Installation #
dart pub add unstory
Quick Start #
Memory history (works everywhere):
import 'package:unstory/unstory.dart';
final history = MemoryHistory();
history.listen((event) {
// Respond to back/forward/go navigations.
});
history.push(Uri.parse('/docs'), state: {'tab': 'intro'});
history.replace(Uri.parse('/docs?tab=api'));
history.back();
Web history:
import 'package:unstory/web.dart';
final browser = BrowserHistory(base: '/app');
final hash = HashHistory();
browser.push(Uri.parse('/account'));
Auto history (web uses browser/hash, otherwise memory):
import 'package:unstory/unstory.dart';
final history = createHistory(
base: '/app',
strategy: .browser,
);
API Overview #
History exposes a small set of primitives:
createHref(Uri)to format a URL for the current history strategy.push(Uri, {state})andreplace(Uri, {state})to update the current entry.go(int),back(), andforward()to move in the history stack.listen((HistoryEvent) => void)to observe pop navigations.dispose()to release resources.createHistory({base, strategy})to select a platform-appropriate history.
Core types:
HistoryLocationwraps aUriplus optionalstate.HistoryEventincludesaction,location, and optionaldelta.HistoryActionis one ofpop,push, orreplace.HistoryStrategyselectsbrowserorhashwhen usingcreateHistoryon web.
Semantics #
pushandreplaceupdatelocationimmediately and do not notify listeners.go/back/forwardmove within the stack and notify listeners with aHistoryEvent.listenreturns a disposer function to remove the listener.
Base Behavior #
The optional base value maps between internal routes and external URLs:
createHrefprepends the base.BrowserHistorystrips the base fromwindow.locationwhen reading.HashHistoryuses the base when building hash-based URLs. If no base is provided, it may fall back to the page<base>element.
Platforms #
MemoryHistoryworks on all platforms.BrowserHistoryandHashHistoryare web-only and live inpackage:unstory/web.dart.
Non-Goals #
Unstory is not a router. It does not do route matching, guards, transitions, or data loading. It only provides a consistent history abstraction.
License #
MIT. See LICENSE.