HarmonyOS鸿蒙Next中Flutter使用shared_preferences存储的,用的时候获取不到token

HarmonyOS鸿蒙Next中Flutter使用shared_preferences存储的,用的时候获取不到token Flutter鸿蒙使用shared_preferences存储的,用的时候获取不到token
鸿蒙IDE版本: 5.1.0
flutter ohos 3.22.0

cke_1312.png

代码规范,在 ios 和 Android 中没有问题
有代码规范示例吗 找不到问题目前 也没有报错


更多关于HarmonyOS鸿蒙Next中Flutter使用shared_preferences存储的,用的时候获取不到token的实战教程也可以访问 https://www.itying.com/category-92-b0.html

4 回复

ArkTS侧同步Dart侧缓存的值。

  1. 在Dart侧设置值。
final SharedPreferencesStorePlatform _prefs = SharedPreferencesOhos();
static const String _prefKey = 'flutter.counter';
_prefs.setValue('Int', _prefKey, 10)
  1. ArkTS侧获取preferences,并同步和处理数据。

注意:shared_preferences中将preferences实例的名称设置为FlutterSharedPreferences,在ArkTS侧调用时需保持一致。

import common from '@ohos.app.ability.common';
import { FlutterPage } from '@ohos/flutter_ohos'
import { preferences } from '@kit.ArkData';

let storage = LocalStorage.getShared()
const EVENT_BACK_PRESS = 'EVENT_BACK_PRESS'

@Entry(storage)
@Component
struct Index {
  private context = this.getUIContext().getHostContext() as common.UIAbilityContext
  @LocalStorageLink('viewId') viewId: string = "";
  private dataPreferences: preferences.Preferences | null = null;

  build() {
   Stack(){
     Column() {
       FlutterPage({ viewId: this.viewId })
     }
     Column(){
       Button("获取preferences").onClick(()=>{
         // FlutterSharedPreferences:通过SharedPreferencesOhosPlugin文件获取
         let promise = preferences.getPreferences( this.context.getApplicationContext(), "FlutterSharedPreferences");
         promise.then((object) => {
           this.dataPreferences = object;
         }).catch((err: ESObject) => {
         })
       })
       Button("获取Dart侧设置的值").onClick(()=>{
         if (this.dataPreferences != null) {
           // key要与Flutter端设置的key一致
           let key = 'flutter.counter';
           let count = this.dataPreferences.getSync(key,0);
           console.log(count.toString());
         }
       })
       
     }
   }
  }

  onBackPress(): boolean {
    this.context.eventHub.emit(EVENT_BACK_PRESS)
    return true
  }
}

Dart侧同步ArkTS侧缓存的值。

  1. 在ArkTS侧缓存值。

注意:shared_preferences中将preferences实例的名称设置为FlutterSharedPreferences,在ArkTS侧调用时需保持一致。

import common from '@ohos.app.ability.common';
import { FlutterPage } from '@ohos/flutter_ohos'
import { preferences } from '@kit.ArkData';

let storage = LocalStorage.getShared()
const EVENT_BACK_PRESS = 'EVENT_BACK_PRESS'

@Entry(storage)
@Component
struct Index {
  private context = this.getUIContext().getHostContext() as common.UIAbilityContext
  @LocalStorageLink('viewId') viewId: string = "";
  private  dataPreferences: preferences.Preferences | null = null;

  build() {
   Stack(){
     Column() {
       FlutterPage({ viewId: this.viewId })
     }
     Column(){
       Button("获取preferences").onClick(()=>{
         // FlutterSharedPreferences:通过SharedPreferencesOhosPlugin文件
         let promise = preferences.getPreferences( this.context.getApplicationContext(), "FlutterSharedPreferences");
         promise.then((object) => {
           this.dataPreferences = object;
         }).catch((err: ESObject) => {
         })
       })

       Button("ArkTS侧缓存值").onClick(()=>{
         if (this.dataPreferences != null) {
           this.dataPreferences.putSync("flutter.counter",1000);
           this.dataPreferences.flushSync();
         }
       })
     }
   }
  }

  onBackPress(): boolean {
    this.context.eventHub.emit(EVENT_BACK_PRESS)
    return true
  }
}
  1. 在Dart侧获取ArkTS侧缓存的值。
final SharedPreferencesStorePlatform _prefs = SharedPreferencesOhos();
static const String _prefKey = 'flutter.counter';
Map<String, Object> maps = await _prefs.getAll();
int count = (maps[_prefKey] as int?) ?? 0;
print("获取ArkTS侧传的值:" + count.toString());

更多关于HarmonyOS鸿蒙Next中Flutter使用shared_preferences存储的,用的时候获取不到token的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


fluwx 这个库看有适配,
fluwx:
git: “https://github.com/OpenFlutter/fluwx.git

这样引入的适配库后,微信登录,支付,分享功能 都用不了,这个需要在鸿蒙中 做什么适配吗?

https://github.com/OpenFlutter/fluwx.git

在HarmonyOS Next中,Flutter的shared_preferences插件依赖平台通道实现数据持久化。由于鸿蒙Next不再兼容Android,其文件系统和存储机制存在差异,可能导致通过shared_preferences存储的token无法正确读取。需要检查鸿蒙的沙箱存储路径是否被正确访问,并确认数据是否成功写入。建议改用鸿蒙原生偏好数据库(@ohos.data.preferences)进行键值对存储,或通过FFI调用鸿蒙本地存储接口实现数据持久化。

在HarmonyOS Next中使用Flutter的shared_preferences插件时遇到token获取不到的问题,可能与鸿蒙系统的存储路径或权限配置有关。建议检查以下方面:

  1. 存储路径差异:鸿蒙系统的应用沙盒路径可能与Android/iOS不同,导致shared_preferences文件未正确创建或读取。确认插件是否适配了HarmonyOS Next的文件系统。

  2. 初始化时机:确保在调用SharedPreferences.getInstance()时已完成初始化,避免在异步操作中未等待完成就直接使用。例如:

    final prefs = await SharedPreferences.getInstance();
    String? token = prefs.getString('token');
    
  3. 数据写入验证:在存储token后,立即读取验证是否成功写入,排除写入失败的可能。

  4. 插件兼容性:确认使用的shared_preferences插件版本支持HarmonyOS Next,并检查是否有已知问题或需额外配置。

如果问题持续,可尝试用鸿蒙系统的原生存储API(如Preferences)替代shared_preferences,以排除插件兼容性问题。

回到顶部