How to use Flutter hooks with ChangeNotifer

Issue

Assume we have a complex Model with ChangeNotifer, so how should I automatically update the UI when model changes?

Here is what I came up with, but looks weird, is it right, is there a better way of doing this?

class Playground extends HookWidget {
  const Playground({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    print("built");

    final dataState = useState(DataClass(0));
    final data = useListenable(dataState.value);

    return Scaffold(
      appBar: AppBar(
        title: Text('Title'),
      ),
      body: ListView(
        children: [
          ListTile(
            title: Text("data: ${data.cnt}"),
            onTap: () {
              data.cnt++;
            },
          ),
        ],
      ),
    );
  }
}

class DataClass with ChangeNotifier {
  int _cnt;
  int get cnt => _cnt;
  set cnt(int val) {
    _cnt = val;
    notifyListeners();
  }

  DataClass(int cnt) : _cnt = cnt;
}

ps: something like useStateListenable or useListenableProvider

Solution

you can combine useState and useListenable to something like useListenableState

T useListenableState<T extends ChangeNotifier>(T data) {
  final state = useState<T>(data);
  return useListenable(state.value);
}

class Playground extends HookWidget {
  const Playground({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    print("built");

    final data = useListenableState(DataClass(0));

    return Scaffold(
      appBar: AppBar(
        title: Text('Title'),
      ),
      body: ListView(
        children: [
          ListTile(
            title: Text("data: ${data.cnt}"),
            onTap: () {
              data.cnt++;
            },
          ),
        ],
      ),
    );
  }
}

class DataClass with ChangeNotifier {
  int _cnt;
  int get cnt => _cnt;
  set cnt(int val) {
    _cnt = val;
    notifyListeners();
  }

  DataClass(int cnt) : _cnt = cnt;
}

Answered By – Ali80

Answer Checked By – Dawn Plyler (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *