rrousselGit / riverpod

A reactive caching and data-binding framework. Riverpod makes working with asynchronous code a breeze.
https://riverpod.dev
MIT License
6.28k stars 955 forks source link

Basic example type errors #430

Closed rhyek closed 3 years ago

rhyek commented 3 years ago

Totally new to flutter, especially riverpod. Using hooks, with a state notifier provider declared like so:

import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:mobile_app/models/adder.dart';

final drinkEventProvider = StateNotifierProvider((ref) => DrinkEventNotifier());

class DrinkEventNotifier extends StateNotifier<List<DrinkEvent>> {
  DrinkEventNotifier() : super([]);

  void add(Adder adder) {
    var adderCopy = Adder(adder.label, adder.amount);
    var event = DrinkEvent(adderCopy);
    state = [...state, event];
  }

  void removeLast() {
    state = state.take(state.length - 1).toList();
  }
}

I have a home widget declared like so:

class Home extends HookWidget {
  final String title;

  Home({Key key, this.title}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final drinkEvents = useProvider(drinkEventProvider);
   ...etc

drinkEvents in that case is typed as dynamic where I would expect it to be List<DrinkEvent>. If I try to see useProvider's result I can see this (no state property): image

Then, trying to access any of DrinkEventNotifier's methods (add, removeLast):

image

pubspec.yaml:

dependencies:
  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.2
  duration: ^3.0.6
  intl: ^0.17.0
  hooks_riverpod: ^0.14.0+1
  flutter_hooks: ^0.16.0
rhyek commented 3 years ago

Figured it all out. Mostly looking at the release notes. Very nice library. Would be helpful if when releasing breaking changes you changed the docs (especially the example on the front page). Feel free to close.

rrousselGit commented 3 years ago

The doc should be up to date.

Where did you find an issue in the doc?

There's also a migration guide https://riverpod.dev/docs/migration/0.13.0_to_0.14.0

wtfiwtz commented 3 years ago

I missed this as well, when attempting to move to Null Safety.

Can it be added to the README.md? It was upgrading the StateNotifierProviders

wtfiwtz commented 3 years ago

Thanks @rrousselGit!