首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Flutter从云firestore加载图像时速度极慢

使用Flutter从云firestore加载图像时速度极慢
EN

Stack Overflow用户
提问于 2020-11-04 13:43:19
回答 2查看 519关注 0票数 0

我正在尝试从云firestore中检索图像数据,但当我这样做时,图像需要极长的时间才能加载,有时根本不会出现。

下面是我检索图像的方法:

代码语言:javascript
复制
final firestore = FirebaseFirestore.instance;
    FirebaseAuth auth = FirebaseAuth.instance;
    Future<String> getProfilePic() async {
      final CollectionReference users = firestore.collection('UserNames');

      final String uid = auth.currentUser.uid;

      final result = await users.doc(uid).get();

      return result.data()['profilepic'];
    }

并显示它:

代码语言:javascript
复制
FutureBuilder(
                  future: getProfilePic(),
                  builder: (_, AsyncSnapshot snapshot) {
                    if (snapshot.connectionState == ConnectionState.waiting) {
                      return Center(
                          child: CircularProgressIndicator(
                        backgroundColor: Palette.lightGreen,
                      ));
                    }
                    return ClipOval(
                      child: Image.network(snapshot.data,
                          height: 100, width: 100, fit: BoxFit.fill),
                    );
                  },
                ),
EN

回答 2

Stack Overflow用户

发布于 2020-11-04 14:34:32

你需要缓存你的图像。

获取此包:

代码语言:javascript
复制
cached_network_image 2.3.3

并更改此代码:

代码语言:javascript
复制
ClipOval(
                  child: Image.network(snapshot.data,
                      height: 100, width: 100, fit: BoxFit.fill),
                );
              },
            ),

添加到此代码

代码语言:javascript
复制
ClipOval(
            child: CachedNetworkImage(
              height: 100,
              width: 100,
              fit: BoxFit.fill,
              imageUrl:
              snapshot.data,
              placeholder: (context, url) => CircularProgressIndicator(),
              errorWidget: (context, url, error) => Icon(Icons.error),
            ),
          ),
票数 0
EN

Stack Overflow用户

发布于 2020-11-04 20:09:12

将以下包添加到您的pubspec.yaml中。

代码语言:javascript
复制
cached_network_image: ^2.3.3

在显示图像的地方尝试以下代码:

代码语言:javascript
复制
             Material(
                child: snapshot.data != null
                    ? CachedNetworkImage(
                        placeholder: (context, url) => Container(
                          child: CircularProgressIndicator(
                            strokeWidth: 1.0,
                            valueColor:
                                AlwaysStoppedAnimation<Color>(themeColor),
                          ),
                          width: 50.0,
                          height: 50.0,
                          padding: EdgeInsets.all(15.0),
                        ),
                        imageUrl: snapshot.data,
                        width: 50.0,
                        height: 50.0,
                        fit: BoxFit.cover,
                      )
                    : Icon(
                        Icons.account_circle,
                        size: 50.0,
                        color: greyColor,
                      ),
                borderRadius: BorderRadius.all(Radius.circular(25.0)),
                clipBehavior: Clip.hardEdge,
              )
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64674782

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档