我正在尝试将图像从 firebase 存储加载到我的应用程序,但是我遇到了一个奇怪的问题,即个人资料页面(加载此图像的位置)一直闪烁。图像加载正常,但整个小部件不断闪烁。经过一些调试后,我已将问题缩小到函数 getProfilePic() 中调用的 setState() ,但是我不知道它是函数本身还是我对所述函数的调用。
PS fileURL 或 picRef.getDownloadURL() 没有问题,因为我也使用随机互联网图像对此进行了测试,并且得到了相同的闪烁。
class profileTab extends StatefulWidget {
@override
_profileTabState createState() => _profileTabState();
}
class _profileTabState extends State<profileTab> {
User user = FirebaseAuth.instance.currentUser;
String _image = "https://picsum.photos/250?image=9";
Reference picRef = FirebaseStorage.instance.ref().child(FirebaseAuth.instance.currentUser.uid);
Future<Widget> getProfilePic() async {
await picRef.getDownloadURL().then((fileURL){
setState(() {
_image = fileURL;
});
});
}
@override
Widget build(BuildContext context) {
getProfilePic();
return StreamBuilder(
stream: FirebaseFirestore.instance.collection('users').doc(user.uid).snapshots(),
builder: (context, snapshot){
if (snapshot.connectionState == ConnectionState.active){
return ListView(
children: <Widget>[
SizedBox(height: 100.0,),
CircleAvatar(
radius: 100.0,
backgroundColor: Colors.lightBlueAccent,
child: ClipOval(
child: SizedBox(
width: 180.0,
height: 180.0,
child: Image.network(_image,fit: BoxFit.fill,),
),
),
),
SizedBox(height: 30.0,),
Center(child: Text("Name: " + snapshot.data.data()['name'],textScaleFactor: 3.0,)),
]
);
}
else {
return CircularProgressIndicator();
}
},
);
}
}