我正在flutter中开发一个天气应用程序,我遇到了一个问题,我正在使用命名路径,我正在模态路径的帮助下访问参数,但在访问构建上下文之外的参数时,它显示错误“未定义名称”参数“。有谁能帮我一下吗?这会有很大的帮助,谢谢你,因为以前我使用构造函数在不同的屏幕之间传递数据,但它太乱了,所以我切换了到命名路由。
我的欢迎屏幕
import 'package:weather_app/utilities/constants.dart';
class WelcomeScreen extends StatefulWidget {
const WelcomeScreen({Key? key}) : super(key: key);
@override
State<WelcomeScreen> createState() => _WelcomeScreenState();
}
class _WelcomeScreenState extends State<WelcomeScreen> {
@override
Widget build(BuildContext context) {
final args = ModalRoute.of(context)!.settings.arguments as Map;
return Scaffold(
body: Container(
color: kPrimaryColor,
child: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: [
const SizedBox(
height: 120,
),
Image.asset(
'images/welcome_screen_pic.png',
height: 250,
),
const SizedBox(
height: 120,
),
// ignore: prefer_const_constructors
Text(
'Discover the Weather\nin Your City',
textAlign: TextAlign.center,
style: kWelcomeScreenTitle,
),
const SizedBox(
height: 20,
),
// ignore: prefer_const_constructors
Text(
textAlign: TextAlign.center,
'Get to know your weather maps and\nradar precipitation forecast',
style: kWelcomeScreenTagLine,
),
const SizedBox(
height: 60,
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextButton(
onPressed: () {
Navigator.pushNamed(context, '/home-screen', arguments: {
'locationWeather': args['locationWeather'], //Here I am transferring data
'aQI': args['aQI'],
'cityName': args['cityName'],
});
},
child: Container(
height: 60,
decoration: const BoxDecoration(
color: kYellowcolor,
borderRadius: BorderRadius.all(Radius.circular(15))),
child: const Center(
child: Text(
'Get Started',
style: kWelcomeScreenButton,
),
),
),
),
)
],
),
),
),
);
}
}
主屏幕的某个部分
const HomeScreen({Key? key}) : super(key: key);
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
WeatherModel weather = WeatherModel();
late int temperature;
late Widget weatherIcon;
late String cityName;
late double windSpeed;
late String description;
late int humidity;
late String time;
List<dynamic> forecastHourlyTemp = [];
List<int> forecastHourlyTime = [];
List<int> forecastHourlyWeatherIcon = [];
List<dynamic> forecastHourlyPOP = [];
List<CardItem> items = [];
@override
void initState() {
updateUI(args['locationWeather'], args['aQI'], args['cityName']); //Here I am getting error like it is saying args is undefined
forecastHourlyTempAdd(args['locationWeather']);
forecastHourlyTimeAdd(args['locationWeather']);
forecastHourlyWeatherIconAdd(args['locationWeather']);
forecastHourlyPOPAdd(args['locationWeather']);
cardItemAdd(0);
super.initState();
}
int i = 1;
void forecastHourlyTempAdd(dynamic weatherData) {
if (i <= 24) {
forecastHourlyTemp.add(weatherData['hourly'][i]['temp']);
i++;
forecastHourlyTempAdd(args['locationWeather']); //Here also
}
}
int j = 1;
void forecastHourlyTimeAdd(dynamic weatherData) {
if (j <= 24) {
forecastHourlyTime.add(weatherData['hourly'][j]['dt']);
j++;
forecastHourlyTimeAdd(args['locationWeather']);//Here also
}
}
int k = 1;
void forecastHourlyWeatherIconAdd(dynamic weatherData) {
if (k <= 24) {
forecastHourlyWeatherIcon
.add(weatherData['hourly'][k]['weather'][0]['id']);
k++;
forecastHourlyWeatherIconAdd(args['locationWeather']);//Here also
}
}
int l = 1;
void forecastHourlyPOPAdd(dynamic weatherData) {
if (l <= 24) {
forecastHourlyPOP.add(weatherData['hourly'][l]['pop']);
l++;
forecastHourlyPOPAdd(args['locationWeather']);//Here also
}
} ```
1条答案
按热度按时间deikduxw1#
也许你可以在initstate中使用post frame回调函数。