nexstate 2.0.4 copy "nexstate: ^2.0.4" to clipboard
nexstate: ^2.0.4 copied to clipboard

A simple dart package for state management inspired by Flutter design patterns.

example/nexstate_example.dart

import 'dart:async';

import 'package:nexstate/nexstate.dart';

Future<void> main() async {
  final userStore = UserStore();
  final subscription = userStore.runAndSubscribe(() => print(userStore.user));

  userStore.login(
    const User(
      firstName: 'John',
      lastName: 'Doe',
      userName: 'john_doe',
    ),
  );

  await userStore.switchAndLogout(
    const User(
      firstName: 'Jane',
      lastName: 'Doe',
      userName: 'jane_doe',
    ),
  );

  Timer.run(subscription.cancel);
}

class User {
  final String firstName;
  final String lastName;
  final String userName;

  const User({
    required this.firstName,
    required this.lastName,
    required this.userName,
  });

  @override
  String toString() {
    return {
      'firstName': firstName,
      'lastName': lastName,
      'userName': userName,
    }.toString();
  }
}

class UserStore extends Store {
  User? user;

  void login(final User newUser) => setState(() => user = newUser);
  void logout() => setState(() => user = null);

  Future<void> switchAndLogout(final User user) async {
    login(user);
    await Future.delayed(const Duration(seconds: 2), logout);
  }
}
2
likes
130
pub points
0%
popularity

Publisher

unverified uploader

A simple dart package for state management inspired by Flutter design patterns.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

nexbounce

More

Packages that depend on nexstate