英文:
refresh API data from a button
问题
I've translated the code part for you. Here it is:
我做了一个天气应用程序,它接收位置并将坐标提供给天气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(
请注意,我只提供了代码的翻译,没有包括注释和文档字符串。如果需要其他部分的翻译,请告诉我。
英文:
I´ve does a weather app, that takes the location and gives the coords to weather API,
and return weather data from the current location of de device,
what i want is to refresh that data with a button, and refresh when i close app and open again later.
any idea to do this?... i´ve implemented a streambuilder to get constant info about the activation state of location in mobile... nut i dont know how to rebuild all the page to execute the methods that give me the information.
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(
答案1
得分: 0
以下是您提供的代码的翻译部分:
刷新您的天气数据,可以使用按钮或在重新打开应用程序时使用有状态小部件和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();
}
}
希望这对您有帮助。如果您需要进一步的解释或有其他问题,请随时提出。
英文:
refresh your weather data with a button or when the app is reopened, use a stateful widget and setState to rebuild the widget and update the data. can you please try below code and let me know if you face any issue.
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();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论