我正在构建一个带有颤音的天气应用程序,我遇到了这个错误&quot“实例成员”窗口'可以在初始化器中访问。 &quot
我正在构建一个带有颤音的天气应用程序,为此,我必须将位置数据从一个屏幕传输到另一个屏幕,即从Splash屏幕到欢迎屏幕,从欢迎屏幕到Main_screen_navigation,该屏幕具有我的底部导航栏,并从此屏幕到HomeScreen。但是问题在于我必须将位置数据传输到构造函数。也许这就是为什么我遇到这个错误的原因。请帮助我解决这个问题,因为我尝试了所有可能的答案,例如使用Init State。 基本上,我想知道如何将WeatherData从底部导航栏屏幕转移到另一个屏幕。请回答这个问题,我正在寻找2天的答案。 谢谢你!
我的主Screen_Navigation
import 'package:flutter/material.dart';
import 'package:weather_app/utilities/constants.dart';
import 'home_screen.dart';
import 'search_screen.dart';
import 'saved_screen.dart';
import 'forecast_screen.dart';
import 'package:curved_navigation_bar/curved_navigation_bar.dart';
class MainScreenNavigation extends StatefulWidget {
MainScreenNavigation({required this.locationWeather});
final locationWeather;
@override
State<MainScreenNavigation> createState() => _MainScreenNavigationState();
}
class _MainScreenNavigationState extends State<MainScreenNavigation> {
int page_number = 0;
List screens = [
HomeScreen(locationWeather: widget.weatherData), //here the error is coming
SearchScreen(),
ForecastScreen(),
SavedScreen(),
];
@override
void initState() {
final weather = widget.locationWeather;
super.initState();
}
final items = [
Icon(
Icons.home,
size: 30,
color: Colors.white,
),
Icon(Icons.search, size: 30, color: Colors.white),
Icon(Icons.auto_graph, size: 30, color: Colors.white),
Icon(Icons.favorite, size: 30, color: Colors.white),
];
@override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: CurvedNavigationBar(
backgroundColor: kPrimaryColor,
color: kPrimaryColor,
buttonBackgroundColor: kSecondaryColor,
animationCurve: Curves.fastOutSlowIn,
height: 60,
items: items,
onTap: (index) {
setState(
() {
page_number = index;
},
);
},
),
body: screens[page_number],
);
}
}
主屏幕
import 'package:flutter/material.dart';
import 'package:weather_app/utilities/constants.dart';
class HomeScreen extends StatefulWidget {
HomeScreen({this.locationWeather});
final locationWeather;
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: kPrimaryColor,
);
}
}
欢迎屏幕
import 'package:flutter/material.dart';
import 'package:weather_app/utilities/constants.dart';
import 'main_screen_navigation.dart';
class WelcomeScreen extends StatefulWidget {
WelcomeScreen({required this.locationWeather});
final locationWeather;
@override
State<WelcomeScreen> createState() => _WelcomeScreenState();
}
class _WelcomeScreenState extends State<WelcomeScreen> {
void getLocationData() {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return MainScreenNavigation(locationWeather: widget.locationWeather);
},
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
color: kPrimaryColor,
child: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
height: 120,
),
Image.asset(
'images/welcome_screen_pic.png',
height: 250,
),
SizedBox(
height: 120,
),
Text(
'Discover the Weather\nin Your City',
textAlign: TextAlign.center,
style: kWelcomeScreenTitle,
),
SizedBox(
height: 20,
),
Text(
textAlign: TextAlign.center,
'Get to know your weather maps and\nradar precipitation forecast',
style: kWelcomeScreenTagLine,
),
SizedBox(
height: 60,
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextButton(
onPressed: () {
getLocationData();
},
child: Container(
height: 60,
child: Center(
child: Text(
'Get Started',
style: kWelcomeScreenButton,
),
),
decoration: BoxDecoration(
color: kYellowcolor,
borderRadius: BorderRadius.all(Radius.circular(15))),
),
),
)
],
),
),
),
);
}
}
I am building a Weather app with Flutter and for this I have to transfer location data from one screen to another screen i.e from splash screen to welcome screen and from welcome screen to main_screen_navigation which has my bottom navigation bar and from this screen to HomeScreen. But the problem is that I have to transfer location data to a constructor. Maybe That's why I am encountering this error. Please help me resolve this one as I have tried all possible answers like using init state.
Basically I want to know how to transfer WeatherData from bottom navigation bar screen to another screens. Please answer this question I am searching for answers from 2 days.
Thank You!
My main screen_navigation
import 'package:flutter/material.dart';
import 'package:weather_app/utilities/constants.dart';
import 'home_screen.dart';
import 'search_screen.dart';
import 'saved_screen.dart';
import 'forecast_screen.dart';
import 'package:curved_navigation_bar/curved_navigation_bar.dart';
class MainScreenNavigation extends StatefulWidget {
MainScreenNavigation({required this.locationWeather});
final locationWeather;
@override
State<MainScreenNavigation> createState() => _MainScreenNavigationState();
}
class _MainScreenNavigationState extends State<MainScreenNavigation> {
int page_number = 0;
List screens = [
HomeScreen(locationWeather: widget.weatherData), //here the error is coming
SearchScreen(),
ForecastScreen(),
SavedScreen(),
];
@override
void initState() {
final weather = widget.locationWeather;
super.initState();
}
final items = [
Icon(
Icons.home,
size: 30,
color: Colors.white,
),
Icon(Icons.search, size: 30, color: Colors.white),
Icon(Icons.auto_graph, size: 30, color: Colors.white),
Icon(Icons.favorite, size: 30, color: Colors.white),
];
@override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: CurvedNavigationBar(
backgroundColor: kPrimaryColor,
color: kPrimaryColor,
buttonBackgroundColor: kSecondaryColor,
animationCurve: Curves.fastOutSlowIn,
height: 60,
items: items,
onTap: (index) {
setState(
() {
page_number = index;
},
);
},
),
body: screens[page_number],
);
}
}
Home Screen
import 'package:flutter/material.dart';
import 'package:weather_app/utilities/constants.dart';
class HomeScreen extends StatefulWidget {
HomeScreen({this.locationWeather});
final locationWeather;
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: kPrimaryColor,
);
}
}
Welcome Screen
import 'package:flutter/material.dart';
import 'package:weather_app/utilities/constants.dart';
import 'main_screen_navigation.dart';
class WelcomeScreen extends StatefulWidget {
WelcomeScreen({required this.locationWeather});
final locationWeather;
@override
State<WelcomeScreen> createState() => _WelcomeScreenState();
}
class _WelcomeScreenState extends State<WelcomeScreen> {
void getLocationData() {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return MainScreenNavigation(locationWeather: widget.locationWeather);
},
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
color: kPrimaryColor,
child: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
height: 120,
),
Image.asset(
'images/welcome_screen_pic.png',
height: 250,
),
SizedBox(
height: 120,
),
Text(
'Discover the Weather\nin Your City',
textAlign: TextAlign.center,
style: kWelcomeScreenTitle,
),
SizedBox(
height: 20,
),
Text(
textAlign: TextAlign.center,
'Get to know your weather maps and\nradar precipitation forecast',
style: kWelcomeScreenTagLine,
),
SizedBox(
height: 60,
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextButton(
onPressed: () {
getLocationData();
},
child: Container(
height: 60,
child: Center(
child: Text(
'Get Started',
style: kWelcomeScreenButton,
),
),
decoration: BoxDecoration(
color: kYellowcolor,
borderRadius: BorderRadius.all(Radius.circular(15))),
),
),
)
],
),
),
),
);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在主屏导航中,您在初始状态以外的初始天气
In mainscreen navigation you initialied weather outside the init state