Last updated:
0 purchases
atomlib
Install #
flutter pub add atomlib
copied to clipboard
Usage #
import 'package:atomlib/atomlib.dart';
import 'package:flutter/material.dart';
// declare store type
class Account {
final String id;
final String email;
final String name;
Account(this.id, this.email, this.name);
}
// declare atom
final accountAtom = Atom<Account?>(null);
// an atom has the following methods
// - get from any where: accountAtom.value
// - set from any where: accountAtom.value = newValue
// - get and listen from a descendant of AtomProvider: accountAtom.of(context)
// - watch for changes from any where: accountAtom.stream.listen((newValue) => print(newValue))
// declare provider
class AuthProvider extends StatelessWidget {
final Widget child;
const AuthProvider({super.key, required this.child});
@override
build(BuildContext context) {
// must attach type to AtomProvider
return AtomProvider<Account?>(
atom: accountAtom,
child: child,
);
}
}
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(MaterialApp(
// wrap your app with AtomProvider
home: AuthProvider(
child: Builder(
builder: (context) {
final account = context.watchAtom(accountAtom);
if (account == null) {
return Text('Not logged in');
}
return Text('Logged in as ${account.name}');
},
),
),
));
}
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.