从画廊或相机挑选后,如何将图像永久保存在颤抖上?设置挑选的图像时,我会遇到此错误

发布于 2025-02-04 20:22:07 字数 437 浏览 0 评论 0原文

在设置挑选的图像时,正在遇到此错误,未来动态不是字符串的子类型。

这就是设置图像

”在此处输入图像描述”

这是使用共享首选项保存图像路径的方式

我在哪里可以出错?

Am getting this error when am setting the picked image, future dynamic is not a subtype of string.

This is how am setting the image

enter image description here

This is how am saving the image path using shared preferences
enter image description here

Where could I have gone wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴情 2025-02-11 20:22:07

那是因为getImagePathfromsharedPreferences不返回字符串?,它返回未来,

您要么在initstate中处理异步任务,要么使用FutureBuilder

请确保设置getimagePathfromsharedPreferences的返回类型

Future<String?> getImagePathFromSharedPreferences() async {
   // ...
}

我将使用FutureBuilder:我将使用FutureBuilder:

FutureBuilder<String?>(
  future: getImagePathFromSharedPreferences(),
  initialData: null,
  builder: (
      BuildContext context,
      AsyncSnapshot<String?> snapshot,
      ) {
    if (snapshot.connectionState != ConnectionState.done) {
      return const CircularProgressIndicator();
    } else if (!snapshot.hasError) {
      return CircleAvatar(
        radius: 45.0,
        backgroundImage: _imageFile != null
          ? Image.file(_imageFile).image
          : snapshot.hasData
            ? FileImage(snapshot.data!)
            : AssetImage(_imageUrl),
      );
    } else {
      return const Text('Error');
    }
  },
),

that's because getImagePathFromSharedPreferences doesn't return String?, it returns a Future

you either handle the async task in the initState or use a FutureBuilder

make sure to set the return type of getImagePathFromSharedPreferences

Future<String?> getImagePathFromSharedPreferences() async {
   // ...
}

I'm gonna use FutureBuilder:

FutureBuilder<String?>(
  future: getImagePathFromSharedPreferences(),
  initialData: null,
  builder: (
      BuildContext context,
      AsyncSnapshot<String?> snapshot,
      ) {
    if (snapshot.connectionState != ConnectionState.done) {
      return const CircularProgressIndicator();
    } else if (!snapshot.hasError) {
      return CircleAvatar(
        radius: 45.0,
        backgroundImage: _imageFile != null
          ? Image.file(_imageFile).image
          : snapshot.hasData
            ? FileImage(snapshot.data!)
            : AssetImage(_imageUrl),
      );
    } else {
      return const Text('Error');
    }
  },
),
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文