frhooks 1.5.2

  • Readme
  • Changelog
  • Installing
  • 62

Frhooks #

Build Status

use react like hooks in flutter.

Motivation #

Have the same thinking of the flutter_hooks motivation. React hooks is more brief to manage local state, and StatefulWidget are verbose. This library brings hooks to flutter. If your are React person, you will like it.

Usage #

useContext #

class MyWidget extends HookWidget {
  Widget build(BuildContext context) {
    BuildContext context = useContext() 
    return Container()

useState #

class MyWidget extends HookWidget {
  Widget build(BuildContext context) {
    StateContainer result = useState(0)
    // result.state
    // result.setState
    return Container()

useEffect #

class MyWidget extends HookWidget {
  Widget build(BuildContext context) {
    useEffect(() {
      // do effect here.
      return () {
        // remove effect here
    return Container()

useCallback #

class MyWidget extends HookWidget {
  Widget build(BuildContext context) {
    var callback = useCallback(() {
      return 1
    }, [])
    // result == 1
    var result = callback()
    return Container()

useMemo #

class MyWidget extends HookWidget {
  Widget build(BuildContext context) {
    var c = useMemo(() => computeExpensiveValue(a, b), [a, b]);
    return Container()

useRef #

class MyWidget extends HookWidget {
  Widget build(BuildContext context) {
    final ref = useRef();
    // free to set and read ref.current
    return Container()

useAnimationController #

class MyWidget extends HookWidget {
  Widget build(BuildContext context) {
    final controller = useAnimationController();
    // controller.forward()
    return Container()

Mixins #

Frhooks aims to replace State completely, also include mixins.

HookAutomaticKeepAliveClientMixin #

Hook version of AutomaticKeepAliveClientMixin.

class MyWidget extends HookWidget with HookAutomaticKeepAliveClientMixin {
  get wantKeepAlive => true;

  Widget build(BuildContext context) {;
    return Container();

1.0.0 #

  • Initial version, created by Stagehand

1.2.0 #

fix: update can rebuild widget correctly.

  • refactor´╝ÜHookElement now extends from ~~ComponentElement~~StatelessElement
  • refactor: HookWidget now extends from ~Widget~StatelessWidget

1.2.2 #

feature: useMemo receive a thunk to lazy compute.

1.2.3 #

feature: hotload will reset hook in Element.

1.2.4 #

fix: reassemble will clear effect.

1.2.5 #

feature: if you insert or remove a hook in your build method, frhooks will rerun the build method and drop the memorized state.

1.2.6 #

fix: frhooks can rerun your hooks correctly after insert or remove one hook.

1.2.7 #

fix: use addPostFrameCallback instead of manual calling didBuild in build.

1.2.8 #

fix: move addPostFrameCallback into willBuild & clean unnecessary variables(currentHookLength & prevHookLength).

1.2.9 #

fix: move _workInProgressHook = null; into willBuild.

1.3.0 #

feature: new hook useRef. feature: new hook useAnimationController.

1.3.1 #

Fix: useState does not return same object. Feature: new hook useAsyncEffect. Feature: new hook useTickerProvider.

1.4.0 #

Feature: an experimental feature HookAutomaticKeepAliveClientMixin added to support mixin on HookWidget.

1.4.1 #

Feature: useAnimationController will auto dispose.

1.4.2 #

Fix: useEffect can memorize changed deps right now.

1.4.3 #

Fix: Pass null to useState should not cause rerender.

1.4.4 #

Fix: clean stashedContext after unmount.

1.5.0 #

Fix: fixed a problem which effect forgot cleanup after recreate. Removal: remove useAsyncEffect.

1.5.1 #

Fix: useEffect not working as expected with multiple effects.

1.5.2 #

Fix: useEffect may be lost.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  frhooks: ^1.5.2

2. Install it

You can install packages from the command line:

with Flutter:

$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:frhooks/frhooks.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jul 2, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

because of import path [frhooks] that is in a package requiring null.

Health suggestions

Fix lib/src/hook.dart. (-0.50 points)

Analysis of lib/src/hook.dart reported 1 hint:

line 2 col 8: Don't import implementation files from another package.

Fix lib/src/hook_element.dart. (-0.50 points)

Analysis of lib/src/hook_element.dart reported 1 hint:

line 13 col 110: Avoid using braces in interpolation when not needed.

Format lib/src/useMemo.dart.

Run flutter format to format lib/src/useMemo.dart.

Maintenance issues and suggestions

Fix analysis_options.yaml.

The analyzer can't parse analysis_options.yaml.

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and frhooks.dart. Packages with multiple examples should provide example/

For more information see the pub package layout conventions.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8
Dev dependencies