やりたいこと
flutterの練習で天気を取得するアプリを作っているのですが、「_select = _kenmei[index];」で受けた値を「 String cityId = '大阪府';」へ再代入したいのですが、うまく渡すことができませんでした。
ご教授お願いいたします。
該当のコード
new.dart
1import 'package:flutter/cupertino.dart'; 2import 'package:flutter/material.dart'; 3 4class NewLocal extends StatefulWidget { 5 @override 6 _NewLocalState createState() => _NewLocalState(); 7} 8 9class _NewLocalState extends State<NewLocal> { 10 String _select = "none"; 11 12 final List<String> _kenmei = [ 13 '北海道', 14 '青森県', 15 '秋田県', 16 '岩手県', 17 '宮城県', 18 '福島県', 19 '茨城県', 20 '栃木県', 21 '群馬県', 22 '埼玉県', 23 '千葉県', 24 '神奈川県', 25 ]; 26 27 @override 28 Widget build(BuildContext context ) { 29 30 return CupertinoPicker( 31 scrollController: FixedExtentScrollController( 32 initialItem: 9, 33 ), 34 itemExtent: 47, 35 children: _kenmei.map((String item) => Text(item)).toList(), 36 onSelectedItemChanged: newLocal, 37 ); 38 } 39 40 String newLocal(int index) { 41 setState(() { 42 _select = _kenmei[index]; 43 print(_select); 44 }); 45 } 46}
main.dart
1import 'package:flutter/material.dart'; 2import 'dart:async'; 3import 'dart:convert'; 4import 'package:http/http.dart' as http; 5import 'package:weather_app/main_widget.dart'; 6 7Future<WeatherInfo> fetchWeather () async{ 8 9 10 WeatherInfo weatherInfo = WeatherInfo(); 11 12 String cityId = '大阪府'; 13 final apiKey = 'xxxxxxxxxxxxxxxxx'; 14 final requestUrl = 'http://api.openweathermap.org/data/2.5/weather?q=${cityId}&lang=ja&units=metric&appid=${apiKey}'; 15 16 final response = await http.get(Uri.parse(requestUrl)); 17 18 if(response.statusCode == 200){ 19 return WeatherInfo.fromJson(jsonDecode(response.body)); 20 }else{ 21 throw Expanded(child: Center(child: Text('読み込みエラー'), 22 ), 23 ); 24 } 25} 26 27class WeatherInfo { 28 final String location; 29 final double temp; 30 final double tempMin; 31 final double tempMax; 32 final String weather; 33 final int humidity; 34 final double windSpeed; 35 36 WeatherInfo({ 37 @required this.location, 38 @required this.temp, 39 @required this.tempMin, 40 @required this.tempMax, 41 @required this.weather, 42 @required this.humidity, 43 @required this.windSpeed, 44 }); 45 46 factory WeatherInfo.fromJson(Map<String, dynamic> json){ 47 return WeatherInfo( 48 location: json['name'], 49 temp: json['main']['temp'], 50 tempMin: json['main']['temp_min'], 51 tempMax: json['main']['temp_max'], 52 weather: json['weather'][0]['description'], 53 humidity: json['main']['humidity'], 54 windSpeed: json['wind']['speed'] 55 ); 56 } 57} 58 59 60void main () => runApp( 61 MaterialApp( 62 title: "お天気アプリ", 63 home:MyApp() 64 ) 65); 66 67class MyApp extends StatefulWidget { 68 @override 69 _MyAppState createState() => _MyAppState(); 70} 71 72class _MyAppState extends State<MyApp> { 73 74 Future<WeatherInfo> futureWeather; 75 76 @override 77 void initState(){ 78 super.initState(); 79 futureWeather = fetchWeather(); 80 } 81 82 @override 83 Widget build(BuildContext context) { 84 return Scaffold( 85 body: FutureBuilder<WeatherInfo>( 86 future: futureWeather, 87 builder: (context, snapshot){ 88 if(snapshot.hasData){ 89 return MainWidget( 90 location: snapshot.data.location, 91 temp: snapshot.data.temp, 92 tempMin: snapshot.data.tempMin, 93 tempMax: snapshot.data.tempMax, 94 weather: snapshot.data.weather, 95 humidity: snapshot.data.humidity, 96 windSpeed: snapshot.data.windSpeed, 97 ); 98 }else if(snapshot.hasError){ 99 return Center( 100 child: Text('スナップショットのエラー'), 101 ); 102 } 103 return CircularProgressIndicator(); 104 }, 105 ) 106 ); 107 } 108}
あなたの回答
tips
プレビュー