flutter 从按钮刷新API数据

1rhkuytd  于 2023-05-19  发布在  Flutter
关注(0)|答案(1)|浏览(159)

我做了一个天气应用程序,它获取位置并将坐标提供给天气API,并从设备的当前位置返回天气数据,
我想要的是用一个按钮刷新那个数据,当我关闭应用程序并稍后再次打开时刷新。
有什么办法吗...我已经实现了一个streambuilder以获得有关移动的中位置的激活状态的恒定信息...螺母我不知道如何重建所有的页面执行的方法,给予我的信息。

class _HomePageState extends State<HomePage> {
  final stream = StreamController<dynamic>();

  WeatherApiService? weatherApi;
  GeolocatorService? geolocatorService;
  NewsService? newsService;

  @override
  void initState() {
    super.initState();
    weatherApi = Provider.of<WeatherApiService>(context, listen: false);
    geolocatorService = Provider.of<GeolocatorService>(context, listen: false);
    newsService = Provider.of<NewsService>(context, listen: false);

    _loadWeatherData();
  }

  void _loadWeatherData() async {
    String coords = await geolocatorService!.getCurrentLocation();

    final hasData = await weatherApi!.getInfoWeatherLocation(coords);

    (hasData) ? true : false;

    stream.sink.add(hasData);
  }

  @override
  Widget build(BuildContext context) {
    final weatherAPI = Provider.of<WeatherApiService>(context);
    final apiResp = weatherAPI;

    return StreamBuilder(
      stream: stream.stream,
      builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return CircularIndicator();
        } else {
          return HomeWidget(
dkqlctbz

dkqlctbz1#

使用按钮刷新您的天气数据,或者在应用程序重新打开时,使用有状态小部件和setState重建小部件并更新数据。你可以请尝试下面的代码,让我知道如果你面临任何问题。

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final stream = StreamController<dynamic>();

  WeatherApiService? weatherApi;
  GeolocatorService? geolocatorService;
  NewsService? newsService;

  @override
  void initState() {
    super.initState();
    weatherApi = Provider.of<WeatherApiService>(context, listen: false);
    geolocatorService = Provider.of<GeolocatorService>(context, listen: false);
    newsService = Provider.of<NewsService>(context, listen: false);

    _loadWeatherData();
  }

  void _loadWeatherData() async {
    String coords = await geolocatorService!.getCurrentLocation();

    final hasData = await weatherApi!.getInfoWeatherLocation(coords);

    (hasData) ? true : false;

    stream.sink.add(hasData);
  }

  void _refreshWeatherData() {
    _loadWeatherData();
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    final weatherAPI = Provider.of<WeatherApiService>(context);
    final apiResp = weatherAPI;

    return Scaffold(
      appBar: AppBar(
        title: const Text('Weather App'),
      ),
      body: StreamBuilder(
        stream: stream.stream,
        builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return const CircularProgressIndicator();
          } else {
            return HomeWidget(
              // Passing weather data to the Home Widget
              weatherData: apiResp.data,
            );
          }
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _refreshWeatherData,
        child: const Icon(Icons.refresh),
      ),
    );
  }

  @override
  void dispose() {
    stream.close();
    super.dispose();
  }
}

相关问题