我正在从选择器页面向个人资料页面发送用户 ID。错误出现一次,然后消失。但该应用程序有效。调试控制台说错误与 selectorPage 有关。我认为 userInfo 函数有问题。
这是我的选择器页面:我将 currentUser 发送到 ProfilePage
final FirebaseAuth auth = FirebaseAuth.instance;
User1 currentUser;
class SelectorPage extends StatefulWidget {
SelectorPage({Key key}) : super(key: key);
@override
_SelectorPageState createState() => _SelectorPageState();
}
class _SelectorPageState extends State<SelectorPage> {
int _selectedIndex = 0;
PageController _pageController;
AuthService authService = new AuthService();
Future<void> userInfo() async {
print("It's work!");
final User gcurrentUser = auth.currentUser;
DocumentSnapshot documentSnapshot =
await usersReference.doc(gcurrentUser.uid).get();
if (!documentSnapshot.exists) {
print("there's no data");
}
setState(() {
currentUser = User1.fromDocument(documentSnapshot);
});
}
@override
void initState() {
setState(() {
userInfo();
});
super.initState();
_pageController = PageController();
}
@override
void dispose() {
_pageController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SizedBox.expand(
child: PageView(
controller: _pageController,
onPageChanged: (index) {
setState(() => _selectedIndex = index);
},
children: <Widget>[
HomePage(),
ChattingPage(),
NotificationPage(),
ProfilePage(userProfileId: currentUser.uid),
],
),
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _selectedIndex,
selectedItemColor: Colors.white,
unselectedItemColor: Colors.grey[600],
backgroundColor: mainColor2,
onTap: _onItemTapped,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
label: '', icon: Icon(Icons.home), backgroundColor: mainColor2),
BottomNavigationBarItem(
label: '', icon: Icon(Icons.chat), backgroundColor: mainColor2),
BottomNavigationBarItem(
label: '',
icon: Icon(Icons.new_releases),
backgroundColor: mainColor2),
BottomNavigationBarItem(
label: '',
icon: Icon(Icons.account_circle_outlined),
backgroundColor: mainColor2),
],
),
);
}
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
userInfo();
_pageController.animateToPage(index,
duration: Duration(milliseconds: 500), curve: Curves.easeOut);
});
}
}
和个人资料页面:
class ProfilePage extends StatefulWidget {
final String userProfileId;
ProfilePage({this.userProfileId});
@override
_ProfilePageState createState() => _ProfilePageState();
}
class _ProfilePageState extends State<ProfilePage> {
AuthService authService = new AuthService();
final String currentOnlineUserId = currentUser?.uid;
@override
void initState() {
super.initState();
}
createProfileTopView() {
return FutureBuilder(
future: usersReference.doc(widget.userProfileId).get(),
builder: (context, dataSnapshot) {
if (!dataSnapshot.hasData) {
return Center(
child: Lottie.asset('assets/images/1.json'),
);
}
User1 user = User1.fromDocument(dataSnapshot.data);
return Padding(
padding: context.paddingLow,
child: Column(
children: [
Row(
children: [
CircleAvatar(
radius: 45.0,
backgroundColor: Colors.grey,
backgroundImage:
CachedNetworkImageProvider(user.photoLink),
),
Expanded(
child: Column(
children: [
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
createColumns("Gönderi", 0),
createColumns("Takipçi", 0),
createColumns("Takip", 0),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [createButton()],
),
],
))
],
),
],
),
);
});
}
Column createColumns(String title, int count) {
return Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
count.toString(),
style: TextStyle(
fontWeight: FontWeight.w300, color: Colors.white, fontSize: 20.0),
),
Container(
margin: EdgeInsets.only(top: 5.0),
child: Text(
title,
style: TextStyle(
fontWeight: FontWeight.w300,
color: Colors.white,
fontSize: 16.0),
),
)
],
);
}
createButton() {
bool ownProfile = currentOnlineUserId == widget.userProfileId;
if (ownProfile) {
return createButtonTitleAndFunction(
title: "Profili düzenle", performFunction: editUserProfile);
}
}
createButtonTitleAndFunction({String title, Function performFunction}) {
return Container(
padding: EdgeInsets.only(top: 3.0),
child: FlatButton(
onPressed: () {},
child: Container(
width: 245.0,
height: 26.0,
child: Text(
title,
style: TextStyle(
fontWeight: FontWeight.w300,
color: Colors.grey,
),
),
alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.black,
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(6.0),
),
),
),
);
}
editUserProfile() {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
EditProfilePage(currentOnlineUserId: currentOnlineUserId)));
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: mainColor,
appBar: AppBar(
backgroundColor: mainColor2,
title: Text(
"Profil",
style: TextStyle(fontWeight: FontWeight.w300),
),
actions: [
IconButton(
icon: Icon(Icons.logout),
onPressed: () {
authService.signOut();
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (context) => LoginPage()));
})
],
),
body: ListView(
children: [
createProfileTopView(),
],
));
}
}
还有我的 UserModal:
class User1 {
final String bio;
final String email;
final String linkedinLink;
final String name;
final String password;
final String photoLink;
final String uid;
final String university;
final String username;
User1({
this.bio,
this.email,
this.linkedinLink,
this.name,
this.password,
this.photoLink,
this.uid,
this.university,
this.username,
});
factory User1.fromDocument(DocumentSnapshot docx) {
return User1(
bio: docx['bio'],
email: docx['email'],
linkedinLink: docx['linkedinLink'],
name: docx['name'],
password: docx['password'],
photoLink: docx['photoLink'],
uid: docx.id,
university: docx['university'],
username: docx['username'],
);
}
}
答案 0 :(得分:1)
问题很可能出在这里:
#search-wrapper {
margin: 10rem auto;
display: flex;
flex-direction: column;
align-items: center;
width: 100%; /* added this */
}
#searchbar {
display: flex;
align-items: center;
width: 100%; /* here */
min-width: 40rem; /* here */
max-width: 60rem; /* here */
border: .1rem solid rgba(0,0,0,.3);
border-radius: 3rem;
margin: 0 2rem;
padding: .75rem 1.25rem;
font-family: "Material Icons";
font-size: 1.8em;
color: rgba(0,0,0,.3);
}
#search-field {
width: 100%; /* and here */
margin: 0 1.5rem;
border: none;
font-family: Helvetica, sans-serif;
}
您试图将 ProfilePage(userProfileId: currentUser.uid),
传递给 currentUser.uid
,但最初 ProfilePage
将是 currentUser
。这解释了错误。
解决方案是仅在有当前用户时才呈现 null
,例如:
ProfilePage