Flutter中如何使用鸿蒙SharedPreferencesOhos

在Flutter项目中如何集成并使用鸿蒙(HarmonyOS)的SharedPreferencesOhos?具体步骤是什么?是否需要额外的插件或配置?能否提供一个简单的代码示例?

2 回复

在Flutter中使用鸿蒙的SharedPreferencesOhos,可以通过shared_preferences_ohos插件实现。具体步骤如下:

  1. 添加依赖:在pubspec.yaml中添加:

    dependencies:
      shared_preferences_ohos: ^0.1.0
    
  2. 导入包

    import 'package:shared_preferences_ohos/shared_preferences_ohos.dart';
    
  3. 读写数据

    // 写入数据
    SharedPreferencesOhos prefs = await SharedPreferencesOhos.getInstance();
    prefs.setString('key', 'value');
    
    // 读取数据
    String? value = prefs.getString('key');
    

支持存储StringintbooldoubleStringList等类型。注意仅适用于鸿蒙平台,其他平台需使用标准shared_preferences

更多关于Flutter中如何使用鸿蒙SharedPreferencesOhos的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在Flutter中使用鸿蒙(HarmonyOS)的SharedPreferencesOhos,需要通过平台通道(Platform Channel) 调用原生鸿蒙API。以下是具体步骤和示例代码:

1. 创建Dart端方法通道

在Flutter中定义方法通道,用于与鸿蒙原生代码通信:

import 'package:flutter/services.dart';

class SharedPreferencesOhos {
  static const MethodChannel _channel = 
      MethodChannel('shared_preferences_ohos');

  // 写入数据
  static Future<bool> setString(String key, String value) async {
    try {
      return await _channel.invokeMethod('setString', {'key': key, 'value': value});
    } on PlatformException catch (e) {
      print("写入失败: ${e.message}");
      return false;
    }
  }

  // 读取数据
  static Future<String?> getString(String key) async {
    try {
      return await _channel.invokeMethod('getString', {'key': key});
    } on PlatformException catch (e) {
      print("读取失败: ${e.message}");
      return null;
    }
  }
}

2. 鸿蒙原生端实现

在鸿蒙工程中创建对应的Ability,实现数据存储逻辑:

Java示例:

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.data.preferences.Preferences;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class SharedPreferencesAbility extends Ability {
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0, "SharedPreferences");
    private Preferences preferences;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        preferences = Preferences.getPreferences(this);
    }

    // 处理Flutter端调用
    @Override
    public void onCommand(Intent intent, boolean restart, int startId) {
        super.onCommand(intent, restart, startId);
        String method = intent.getStringParam("method");
        if ("setString".equals(method)) {
            String key = intent.getStringParam("key");
            String value = intent.getStringParam("value");
            preferences.putString(key, value).flush();
        } else if ("getString".equals(method)) {
            String key = intent.getStringParam("key");
            String value = preferences.getString(key, "");
            // 返回结果给Flutter
        }
    }
}

3. 在Flutter中使用

// 写入数据
await SharedPreferencesOhos.setString("username", "HarmonyUser");

// 读取数据
String? name = await SharedPreferencesOhos.getString("username");
print("用户名: $name"); // 输出: 用户名: HarmonyUser

注意事项:

  1. 包名配置:确保Flutter方法通道名称与鸿蒙端注册一致
  2. 权限申请:鸿蒙需在config.json中添加数据存储权限
  3. 数据类型:可扩展支持intbool等类型
  4. 错误处理:添加完整的异常捕获机制

此方案通过平台通道桥接Flutter与鸿蒙原生API,实现数据持久化功能。实际开发时建议封装成完整插件,便于复用。

回到顶部