【问题标题】:Flutter Error: type '_Uri' is not a subtype of type 'String'颤振错误:类型“_Uri”不是“字符串”类型的子类型
【发布时间】:2021-08-09 02:03:22
【问题描述】:

我在保存从 firebase 实时数据库中检索图片的 URL 时收到此错误

type '_Uri' is not a subtype of type 'String'

我所做的是首先将图像 URL 存储在 firebase 存储中,然后将 URL 保存在我的 firebase 实时数据库中的当前用户信息下,但是当我从 Db 访问个人资料图像 URL 时,它给了我错误上面提到过。

我的屏幕视图如下所示,我想在其中显示用户选择作为当前用户个人资料图像的图像,直到用户更改图像。

我的函数代码是:

FirebaseAuth firebaseAuth = FirebaseAuth.instance;
  final ref = FirebaseDatabase.instance.reference();
  final fb = FirebaseDatabase.instance;

Future<Uri> getPicture() async {
    User cuser = await firebaseAuth.currentUser;
    return ref
        .child('User_data')
        .child(cuser.uid)
        .once()
        .then((DataSnapshot snap) {
      final String profileURL = snap.value['profile_photo'].toString();
      var myUri = Uri.parse(profileURL);
      print(myUri);
      return myUri;
    });
  }

从数据库中显示和检索图像的代码:

Center(
              child: Stack(
                alignment: Alignment.center,
                children: <Widget>[
                  FutureBuilder(
                    future: getPicture(),
                      builder: (context, snapshot)
                      {
                      if (snapshot.hasData)
                        {
                          return CircleAvatar(
                            radius: 50,
                            backgroundImage: snapshot.data == null
                                ? AssetImage("assets/images/avatar.jpg")
                                : Image.network(snapshot.data),
                          );
                        }

                      }
                  ),
                  Positioned(
                    bottom: 1,
                    right: 1,
                    child: IconButton(
                      icon: Icon(
                        Icons.add_circle,
                        color: const Color(0xffd4d411),
                        size: 30,
                      ),
                      onPressed: () {
                        showModalBottomSheet(
                          context: context,
                          builder: ((builder) => bottomSheet(context)),
                        );
                      },
                      //color: const Color(0xffd4d411),
                    ),
                  )
                ],
              ),
            ),

【问题讨论】:

    标签: firebase flutter dart firebase-realtime-database firebase-storage


    【解决方案1】:

    Image.network 采用 String 作为位置参数。但是你提供了一个Uri 对象。

    把你的getPicture改成这个,

    Future<String> getPicture() async {
      User cuser = await firebaseAuth.currentUser;
      return ref
        .child('User_data')
        .child(cuser.uid)
        .once()
        .then((DataSnapshot snap) {
          return snap.value['profile_photo'].toString();
        });
    }
    

    现在,您将在snapshot.data 中收到String

    接下来,像这样将您的Image.Network 更改为NetworkImage

    backgroundImage: snapshot.data == null
      ? AssetImage("assets/images/avatar.jpg")
      : NetworkImage(snapshot.data),
    

    【讨论】:

    • type 'Image' is not a subtype of type 'ImageProvider&lt;Object&gt;?' 执行此操作时出现此错误。请帮帮我?
    • 很高兴能帮上忙。 :)
    猜你喜欢
    • 2021-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-23
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 2020-01-01
    相关资源
    最近更新 更多