flutter_session 0.1.1 flutter_session: ^0.1.1 copied to clipboard
Adds session support to Flutter. Works with mobile, web and (hopefully) desktop builds.
import 'package:flutter/material.dart';
import 'package:flutter_session/flutter_session.dart';
class Data {
final int id;
final String data;
Data({this.data, this.id});
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = Map<String, dynamic>();
data["id"] = id;
data["data"] = this.data;
return data;
}
}
class Page1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Material(
child: FutureBuilder(
future: saveData(context),
builder: (context, snapshot) {
return Text("You will not see this");
}),
);
}
Future<void> saveData(context) async {
Data myData = Data(data: "Lorem ipsum, something, something...", id: 1);
await FlutterSession().set('myData', myData);
Navigator.push(context, MaterialPageRoute(builder: (_context) => Page2()));
}
}
class Page2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Material(
child: FutureBuilder(
future: FlutterSession().get('myData'),
builder: (context, snapshot) {
return Text(snapshot.hasData
? snapshot.data['id'].toString() + "|" + snapshot.data['data']
: 'Loading...');
}));
}
}