当我从显示用户已加载的内容重新启动时,但是当我更改用户时,当我注销并输入另一个用户并进入创建帖子屏幕时,我收到此错误:
════════ Exception caught by widgets library ═══════════════════════════════════
The method '[]' was called on null.
Receiver: null
Tried calling: []("email")
The relevant error-causing widget was
StreamBuilder<DocumentSnapshot<Map<String, dynamic>>>
这没有任何意义,因为正在加载电子邮件,以至于您正在登录并且它在firestore中。这是加载 Circle 头像和名称的 Upload 类中的我的 StreamBuilder。
final String currentUserId;
const Upload({Key key, this.currentUserId}) : super(key: key);
StreamBuilder<DocumentSnapshot<Map<String, dynamic>>>(
stream: usersRef.doc(widget.currentUserId).snapshots(),
builder: (context,
AsyncSnapshot<DocumentSnapshot<Map<String, dynamic>>>
snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation(Colors.blue),
),
);
}
UserModel userModel = UserModel.fromDoc(snapshot.data);
return Container(
child: SizedBox(
height: 100,
child: ListView(
children: [
Container(
padding: EdgeInsets.symmetric(horizontal: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
CircleAvatar(
radius: 22,
backgroundImage: userModel
.profilePicture.isEmpty
? AssetImage(
'assets/images/placeholder.png')
: NetworkImage(
userModel.profilePicture),
),
SizedBox(
width: 10,
),
Text(
userModel.username,
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.bold),
),
],
),
],
))
],
这是我的班级之家。
class Home extends StatefulWidget {
final String currentUserId;
const Home({Key key, this.currentUserId}) : super(key: key);
@override
void initState() {
super.initState();
pageController = PageController();
}
@override
void dispose() {
pageController.dispose();
super.dispose();
}
onPageChanged(int pageIndex) {
setState(() {
this.pageIndex = pageIndex;
});
}
onTap(int pageIndex) {
pageController.animateToPage(
pageIndex,
duration: Duration(milliseconds: 200),
curve: Curves.easeInOut
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: PageView(
children: [
Timeline(currentUserId: widget.currentUserId),
ActivityFeed(),
Upload(currentUserId: widget.currentUserId),
Search(),
Profile(currentUserId: widget.currentUserId),
],
controller: pageController,
onPageChanged: onPageChanged,
physics: NeverScrollableScrollPhysics(),
),
bottomNavigationBar: CupertinoTabBar(
currentIndex: pageIndex,
onTap: onTap,
activeColor: Theme.of(context).primaryColor,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.whatshot)),
BottomNavigationBarItem(
icon: Icon(Icons.notifications)),
BottomNavigationBarItem(
icon: Icon(Icons.camera_alt, size: 35.0,)),
BottomNavigationBarItem(
icon: Icon(Icons.search)),
BottomNavigationBarItem(
icon: Icon(Icons.account_circle)),
],
),
);
}
}
我的用户模型。
class UserModel {
String id;
String username;
String email;
String profilePicture;
String coverImage;
String bio;
// String displayName;
UserModel({
this.id,
this.username,
this.email,
this.profilePicture,
this.coverImage,
// this.displayName,
this.bio,
});
factory UserModel.fromDoc(DocumentSnapshot<Map<String, dynamic>> doc) {
return UserModel(
id: doc.id,
email: doc.data()['email'],
username: doc.data()['username'],
profilePicture: doc.data()['profilePicture'],
coverImage: doc.data()['coverImage'],
// displayName: doc.data()['displayName'],
bio: doc.data()['bio'],
);
}
}
我的主要。
Widget getScreenId() {
return StreamBuilder<User>(
stream: FirebaseAuth.instance.authStateChanges(),
builder: (BuildContext context, AsyncSnapshot<User> snapshot) {
if (snapshot.hasData) {
return Home(currentUserId: snapshot.data.uid);
} else {
return LoginPage();
}
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme:
ThemeData(primarySwatch: Colors.deepOrange, accentColor: Colors.teal),
debugShowCheckedModeBanner: false,
home: getScreenId(),
);
}
}