Sure, here’s some basic code to get you started on creating a weather app in Flutter:

import ‘package:flutter/material.dart’;
import ‘package:http/http.dart’ as http;
import ‘dart:convert’;

class WeatherApp extends StatefulWidget {
@override
_WeatherAppState createState() => _WeatherAppState();
}

class _WeatherAppState extends State<WeatherApp> {
String cityName = “”;
var temperature;

Future<void> getWeather() async {
var response = await http.get(
“http://api.openweathermap.org/data/2.5/weather?q=$cityName&appid=YOUR_API_KEY”);
var data = json.decode(response.body);

setState(() {
temperature = data[“main”][“temp”];
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(“Weather App”),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
onChanged: (value) {
cityName = value;
},
decoration: InputDecoration(
hintText: “Enter city name”,
),
),
),
RaisedButton(
onPressed: () {
getWeather();
},
child: Text(“Get Weather”),
),
temperature == null
? Text(“Enter city name”)
: Text(“Temperature: ${temperature.toString()}”),
],
),
);
}
}

In the code above, the getWeather() method is used to fetch weather data from the OpenWeatherMap API using the http package. The city name is entered by the user in the TextField widget. The temperature data is then displayed on the screen. Note that you will need to replace YOUR_API_KEY in the API URL with your actual API key, which you can get from the OpenWeatherMap website.

 

Facebook Comments Box
0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments