flutter 未为类型“AsyncValue”定义方法“requestWeather”

qij5mzcb  于 2023-08-07  发布在  Flutter
关注(0)|答案(1)|浏览(156)

我在试着理解Flutter Riverpod。我可以从服务中获取和显示数据,并了解使用AsyncValue的好处。但是,当我尝试调用StateNotifier中的 *requestWeather * 函数时,该函数应依次调用 *retrieveWeather *,并获取新位置的天气信息。但我得到了这个错误。

未为类型“AsyncValue’定义方法“requestWeather”。尝试将名称更正为现有方法的名称,或定义一个名为“requestWeather”的方法。dartundefined_method

从main.dart,我试图使用Provider访问requestWeather函数。

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定义了requestWeather函数。

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());
    }
  }
}

jogvjijk

jogvjijk1#

当使用StateNotifier并尝试访问这些方法时,它需要像下面这样的provider.notifier

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

字符串
我在访问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'),
          )
        ],
      ),
    );
  }
}

相关问题