Flutter Error: type '_Uri' is not a subtype of type 'String'

Flutter Error: type '_Uri' is not a subtype of type 'String'

我在保存从 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),
                    ),
                  )
                ],
              ),
            ),

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),