uistate 1.0.2 copy "uistate: ^1.0.2" to clipboard
uistate: ^1.0.2 copied to clipboard

outdated

The cleanest way of representing UIState in a flutter widget.

UIState #

Cleanest way of representing UI state in a flutter widget.

Loading Success Failure

Usage #

Get a live/stream representation of your data:

UIState<String> state = Provider.of<ViewModel>(context).state;

Inspired by kotlin's inline switch:

The when returns the widget of the current state of your state variable

state.when(
    success: (event) => successWidget(event.value),
    failure: (event) => failureWidget(event.errorMessage),
    loading: (event) => loadingSpinner(),
)

build method example: #

@override
  Widget build(BuildContext context) {
    UIState<String> state = Provider.of<ViewModel>(context).state;

    return Scaffold(
      body: Container(
        margin: EdgeInsets.all(20),
        child: Center(
          child: state.when(
            success: (event) => successWidget(event.value),
            failure: (event) => failureWidget(event.errorMessage),
            loading: (event) => loadingSpinner(),
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.adjust_sharp),
        onPressed: () {
          Provider.of<ViewModel>(context, listen: false).fetchUser();
        },
      ),
    );
  }

The ChangeNotifier (ViewModel) side of it #

The state variable will assume the loading/failure/success state of the data request:

class ViewModel extends ChangeNotifier {
  final UsernameRepository repository;
  ViewModel(this.repository);

  UIState<String> state = Loading();

  fetchUser() async {
    try {
      String username = await repository.getUsername();
      state = Success(username);
    } catch (error) {
      state = Failure(error.toString());
    }

    notifyListeners();
  }
}

Install #

dependencies:
  uistate: ^1.0.2

The import:

import 'package:uistate/uistate.dart';

or follow these steps


Made with ♥ by Cesar Ferreira

5
likes
0
pub points
23%
popularity

Publisher

unverified uploader

The cleanest way of representing UIState in a flutter widget.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter, meta

More

Packages that depend on uistate