0

I am trying to understand Flutter Riverpod. I can fetch and display data from the service and understand the benefits of using AsyncValue. However when I try to call the requestWeather function inside a StateNotifier which should call the retrieveWeather in turn and get the weather information for a new location. But I get this error.

The method 'requestWeather' isn't defined for the type 'AsyncValue'. Try correcting the name to the name of an existing method, or defining a method named 'requestWeather'.dartundefined_method

From main.dart where I am trying to access the requestWeather function using Provider.

    class InputLocationBox extends ConsumerWidget {
  String result = "";

  // final weatherListState = watch(weatherListControllerProvider);
  @override
  Widget build(BuildContext context, ScopedReader watch) {
    return Container(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          new TextField(
              decoration: new InputDecoration(hintText: "Type in here"),
              //onChanged is called whenever we add or delete something on Text Field
              onChanged: (String str) {
                result = str;
                // watch(weatherRepositoryProvider).retrieveWeather(location: str);
              }),
          //displaying input text
          new ElevatedButton(
            onPressed: () {
              context
                  .read(weatherListControllerProvider)
                  .requestWeather(location: result);
            },
            child: Text('Get user info'),
          )
        ],
      ),
    );
  }
}

Weather_list_controller.dart which defines requestWeather function.

 class WeatherListController extends StateNotifier<AsyncValue<Weather>> {
  final Reader _read;

  WeatherListController(this._read) : super(AsyncValue.loading()) {
    requestWeather();
  }

  Future<void> requestWeather({bool isRefreshing = false, String? loc}) async {
    if (isRefreshing) state = AsyncValue.loading();
    if (loc == null) {
      loc = 'Los Angeles';
    }

    try {
      final weathers =
          await _read(weatherRepositoryProvider).retrieveWeather(location: loc);
      if (mounted) {
        state = AsyncValue.data(weathers);
        // print(state);
      }
      // return weathers;
    } catch (e) {
      throw Exception(
          'Something went wrong in WeatherListController !! ' + e.toString());
    }
  }
}
Walker
  • 891
  • 2
  • 11
  • 26
  • Does this answer your question? [The type 'StateNotifierProvider' is declared with 2 type parameters, but 1 type arguments were given](https://stackoverflow.com/questions/67233449/the-type-statenotifierprovider-is-declared-with-2-type-parameters-but-1-type) – Alex Hartford May 06 '21 at 15:33

1 Answers1

0

When using StateNotifier and trying to access the methods, it requires provider.notifier like this

context
              .read(weatherListControllerProvider.notifier)
              .requestWeather(loc: result);

the widget where I was trouble having access to the requestWeather

class InputLocationBox extends ConsumerWidget {
  String result = "";

  @override
  Widget build(BuildContext context, ScopedReader watch) {
    final weatherListState = watch(weatherListControllerProvider.notifier);
    return Container(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          new TextField(
              decoration: new InputDecoration(hintText: "Type in here"),
              //onChanged is called whenever we add or delete something on Text Field
              onChanged: (String str) {
                result = str;
                // watch(weatherRepositoryProvider).retrieveWeather(location: str);
              }),
          //displaying input text
          new ElevatedButton(
            onPressed: () {
              // context
              //     .read(weatherListControllerProvider)
              //     .requestWeather(location: result);
              print(result);
              context
                  .read(weatherListControllerProvider.notifier)
                  .requestWeather(loc: result);
            },
            child: Text('Get Weather'),
          )
        ],
      ),
    );
  }
}
Walker
  • 891
  • 2
  • 11
  • 26