【问题标题】:firestore image not load to profile pagefirestore 图像未加载到个人资料页面
【发布时间】:2020-05-27 16:36:29
【问题描述】:

当我尝试从 Firestore 数据库中提取配置文件图像时,它在配置文件页面上显示以下错误 '包:颤振/src/绘画 _network_image_io.dart': 断言失败:line22 pos14:'url!=null':是 不对。 也可以看看: https://flutter.dev/docs/testing/errors

虽然如果我按 ctrl+s 会出现此错误,但它会加载配置文件页面。这是我的代码。

 String profilePicUrl;
 @override
 void initState() {

super.initState();
FirebaseAuth.instance.currentUser().then((user){
  profilePicUrl=user.photoUrl;
}).catchError((e){
  print(e);
});

} 然后我把profilpicUrl放到networkImage里面,如下

    Container(
                width: 150.0,
                height: 150.0,
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: NetworkImage(profilePicUrl),
                    fit: BoxFit.cover,
                  ),
                  borderRadius: BorderRadius.all(Radius.circular(75.0)),
                  boxShadow: [
                    BoxShadow(
                      blurRadius:7.0,
                      color:Colors.black
                      )
                      ]

                ),
              ),

每次我运行应用程序时,我都必须按 ctrl+s 以防止错误并加载配置文件页面

【问题讨论】:

  • 您共享的代码是从 Firebase 身份验证读取用户的个人资料图片,而不是从 Firestore。

标签: image firebase flutter firebase-authentication


【解决方案1】:

图像没有及时加载,首先调用initState,然后在接收图像之前调用build方法。因此,您可以使用FutureBuilder

Container(
        width: 150.0,
        height: 150.0,
        child: FutureBuilder(
          builder: (context, AsyncSnapshot<FirebaseUser> snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              return Image.network(profilePicUrl, fit: BoxFit.cover);
            } else if (snapshot.connectionState == ConnectionState.none) {
              return Text("No data");
            }
            return CircularProgressIndicator();
          },
          future: FirebaseAuth.instance.currentUser(),
        ),
      ),

【讨论】:

  • @vindi96 因为答案对您有帮助,请点赞,以便其他人知道它有帮助,谢谢!
猜你喜欢
  • 1970-01-01
  • 2022-10-17
  • 2015-11-04
  • 2014-10-09
  • 2015-10-08
  • 2019-02-16
  • 1970-01-01
  • 1970-01-01
  • 2016-11-26
相关资源
最近更新 更多