除非在 VScode(ctrl + s) 上使用热重载,否则 Flutter 应用屏幕页面不会自动加载内容
Flutter app screen page does not automatic load content unless uisng hot reload on VScode(ctrl + s)
嗨,我是 android vscode 的新手,我正在寻找解决方案,在我将注册帐户从 firebase 数据库登录到我正在构建的应用程序后,它将加载已注册的完整内容个人资料页面上的用户名以及电子邮件。我正在使用底部导航栏,如果我在应用程序中单击不同的按钮并返回到个人资料页面,则发布的文本将变为空。但是,如果我将其保留在配置文件页面上并在 vs 代码上按 ctrl+s(热重载),它将显示数据。
这是我的个人资料代码(这与主页不同)
这是热重载后的截图
这是在点击其他按钮之后
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:stock_market_tracker_v3/Models/User_Models.dart';
import 'package:stock_market_tracker_v3/Screens/LoginPage.dart';
class ProfilePage extends StatefulWidget {
const ProfilePage({Key? key}) : super(key: key);
@override
State<ProfilePage> createState() => _ProfilePageState();
}
class _ProfilePageState extends State<ProfilePage> {
User? user = FirebaseAuth.instance.currentUser;
UserModel loggedInUser = UserModel();
@override
void initState() {
// TODO: implement initState
FirebaseFirestore.instance
.collection("Users")
.doc(user!.uid)
.get()
.then((value){
this.loggedInUser = UserModel.fromMap(value.data());
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Welcome!"),
centerTitle: true,
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
SizedBox(
height: 150,
child: Image.asset("Asset/images/levi.gif", fit: BoxFit.contain),
),
const Text(
"Welcome back",
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
const SizedBox(height: 10,),
Text("${loggedInUser.firstName} ${loggedInUser.lastname}",
style: TextStyle(color: Colors.black54,fontWeight: FontWeight.w500),),
Text("${loggedInUser.email}",
style: TextStyle(color: Colors.black54,fontWeight: FontWeight.w500),),
const SizedBox(height: 15,
),
ActionChip(label: const Text("Logout"), onPressed: (){
logout(context);
}),
],
),
),
),
);
}
Future<void> logout(BuildContext context)async
{
await FirebaseAuth.instance.signOut();
Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context) => LoginPage()));
}
} '''
@override
void initState() {
// TODO: implement initState
super.initState();
loadData();
}
loadData() async {
await FirebaseFirestore.instance
.collection("Users")
.doc(user!.uid)
.get()
.then((value){
setState((){
this.loggedInUser = UserModel.fromMap(value.data());
});
});
}
试试这个
嗨,我是 android vscode 的新手,我正在寻找解决方案,在我将注册帐户从 firebase 数据库登录到我正在构建的应用程序后,它将加载已注册的完整内容个人资料页面上的用户名以及电子邮件。我正在使用底部导航栏,如果我在应用程序中单击不同的按钮并返回到个人资料页面,则发布的文本将变为空。但是,如果我将其保留在配置文件页面上并在 vs 代码上按 ctrl+s(热重载),它将显示数据。 这是我的个人资料代码(这与主页不同)
这是热重载后的截图
这是在点击其他按钮之后
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:stock_market_tracker_v3/Models/User_Models.dart';
import 'package:stock_market_tracker_v3/Screens/LoginPage.dart';
class ProfilePage extends StatefulWidget {
const ProfilePage({Key? key}) : super(key: key);
@override
State<ProfilePage> createState() => _ProfilePageState();
}
class _ProfilePageState extends State<ProfilePage> {
User? user = FirebaseAuth.instance.currentUser;
UserModel loggedInUser = UserModel();
@override
void initState() {
// TODO: implement initState
FirebaseFirestore.instance
.collection("Users")
.doc(user!.uid)
.get()
.then((value){
this.loggedInUser = UserModel.fromMap(value.data());
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Welcome!"),
centerTitle: true,
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
SizedBox(
height: 150,
child: Image.asset("Asset/images/levi.gif", fit: BoxFit.contain),
),
const Text(
"Welcome back",
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
const SizedBox(height: 10,),
Text("${loggedInUser.firstName} ${loggedInUser.lastname}",
style: TextStyle(color: Colors.black54,fontWeight: FontWeight.w500),),
Text("${loggedInUser.email}",
style: TextStyle(color: Colors.black54,fontWeight: FontWeight.w500),),
const SizedBox(height: 15,
),
ActionChip(label: const Text("Logout"), onPressed: (){
logout(context);
}),
],
),
),
),
);
}
Future<void> logout(BuildContext context)async
{
await FirebaseAuth.instance.signOut();
Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context) => LoginPage()));
}
} '''
@override
void initState() {
// TODO: implement initState
super.initState();
loadData();
}
loadData() async {
await FirebaseFirestore.instance
.collection("Users")
.doc(user!.uid)
.get()
.then((value){
setState((){
this.loggedInUser = UserModel.fromMap(value.data());
});
});
}
试试这个