import 'package:flutter/material.dart';
import 'widgets/main_widget.dart';
import 'package:http/http.dart' as http;
import 'dart:async';
import 'dart:convert';
Future fetchWeather() async {
final zipCode = "180007";
final apiKey = "d415dc7d3cae2e2b8a722dca06eb9e88";
final requestUrl =
"http://api.openweathermap.org/data/2.5/ ... id={apiKey}";
final response = await http.get(Uri.parse(requestUrl));
return WeatherInfo.fromJson(jsonDecode(response.body));
}
class WeatherInfo {
final String location;
final double temp;
final double tempMin;
final double tempMax;
final String weather;
final int Humidity;
final double windspeed;
WeatherInfo(
{required this.location,
required this.temp,
required this.tempMin,
required this.tempMax,
required this.weather,
required this.Humidity,
required this.windspeed});
factory WeatherInfo.fromJson(Map json) {
return WeatherInfo(
location: json['name'],
temp: json['main']['temp'],
tempMin: json['main']['temp_min'],
tempMax: json['main']['temp_max'],
weather: json['weather']['description'],
Humidity: json['main']['humidity'],
windspeed: json['wind']['speed']);
}
}
void main() => runApp(MaterialApp(title: "Weather App", home: MyApp()));
class MyApp extends StatefulWidget {
@override
State createState() {
return _MyApp();
}
}
class _MyApp extends State {
late Future futureWeather;
@override
void initState() {
super.initState();
futureWeather = fetchWeather();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder(
future: futureWeather,
builder: (context, Snapshot) {
if (Snapshot.hasData) {
return MainWidget(
location: Snapshot.data!.location,
temp: Snapshot.data!.temp,
tempMin: Snapshot.data!.tempMin,
tempMax: Snapshot.data!.tempMax,
weather: Snapshot.data!.weather,
Humidity: Snapshot.data!.Humidity,
windspeed: Snapshot.data!.windspeed);
} else if (Snapshot.hasError) {
return Center(
child: Text("${Snapshot.error}"),
);
}
return CircularProgressIndicator();
}));
}
}
Подробнее здесь: https://stackoverflow.com/questions/705 ... -type-null
Ожидалось значение типа «String», но получено значение типа «Null». ⇐ Android
Форум для тех, кто программирует под Android
-
Anonymous
1737024100
Anonymous
import 'package:flutter/material.dart';
import 'widgets/main_widget.dart';
import 'package:http/http.dart' as http;
import 'dart:async';
import 'dart:convert';
Future fetchWeather() async {
final zipCode = "180007";
final apiKey = "d415dc7d3cae2e2b8a722dca06eb9e88";
final requestUrl =
"http://api.openweathermap.org/data/2.5/weather?zip={zipCode},IN&units=imperial&appid={apiKey}";
final response = await http.get(Uri.parse(requestUrl));
return WeatherInfo.fromJson(jsonDecode(response.body));
}
class WeatherInfo {
final String location;
final double temp;
final double tempMin;
final double tempMax;
final String weather;
final int Humidity;
final double windspeed;
WeatherInfo(
{required this.location,
required this.temp,
required this.tempMin,
required this.tempMax,
required this.weather,
required this.Humidity,
required this.windspeed});
factory WeatherInfo.fromJson(Map json) {
return WeatherInfo(
location: json['name'],
temp: json['main']['temp'],
tempMin: json['main']['temp_min'],
tempMax: json['main']['temp_max'],
weather: json['weather']['description'],
Humidity: json['main']['humidity'],
windspeed: json['wind']['speed']);
}
}
void main() => runApp(MaterialApp(title: "Weather App", home: MyApp()));
class MyApp extends StatefulWidget {
@override
State createState() {
return _MyApp();
}
}
class _MyApp extends State {
late Future futureWeather;
@override
void initState() {
super.initState();
futureWeather = fetchWeather();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder(
future: futureWeather,
builder: (context, Snapshot) {
if (Snapshot.hasData) {
return MainWidget(
location: Snapshot.data!.location,
temp: Snapshot.data!.temp,
tempMin: Snapshot.data!.tempMin,
tempMax: Snapshot.data!.tempMax,
weather: Snapshot.data!.weather,
Humidity: Snapshot.data!.Humidity,
windspeed: Snapshot.data!.windspeed);
} else if (Snapshot.hasError) {
return Center(
child: Text("${Snapshot.error}"),
);
}
return CircularProgressIndicator();
}));
}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/70503708/expected-a-value-of-type-string-but-got-one-of-the-type-null[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия