Use of MMKV in dart

The Dart end

pubspec.yaml

Dependencies: MMKV: "> = 1.2.8"...Copy the code

ios

To avoid conflicts with the local library name “libmmKV.so” on iOS, we need to change the plug-in name “MMKV” to “mmkvflutter”.

For the pure Flutter application, add this function fix_mmkv_plugin_name () to the ios/Podfile and call it before calling any flutter_xxx () function. Run pod install.

def fix_mmkv_plugin_name(flutter_application_path) is_module = false plugin_deps_file = File.expand_path(File.join(flutter_application_path, '.. ', '.flutter-plugins-dependencies')) if not File.exists? (plugin_deps_file) is_module = true; plugin_deps_file = File.expand_path(File.join(flutter_application_path, '.flutter-plugins-dependencies')) end plugin_deps = JSON.parse(File.read(plugin_deps_file)).dig('plugins', 'ios') || [] plugin_deps.each do |plugin| if plugin['name'] == 'mmkv' || plugin['name'] == 'mmkvflutter' require File.expand_path(File.join(plugin['path'], 'tool', 'mmkvpodhelper.rb')) mmkv_fix_plugin_name(flutter_application_path, Is_module) return end end raise "Can't find any MMKV plug-in dependencies. If you are running the Pod installation manually, be sure to run the flutter pub get" end fix_mmkv_plugin_name(file.dirname (file.realPath (__FILE__))) first.Copy the code
To use flutter as a module for an existing iOS App, add the above function fix_mmkv_plugin_name () to the Podfile of the iOS App, calling it before calling any flutter_xxx () function. Run Pod Install and we're ready to go. Def fix_mmkv_plugin_name (flutter_application_path)..... end flutter_application_path = 'path/to/your/flutter_module' fix_mmkv_plugin_name(flutter_application_path)Copy the code

Android

If you have previously used com.0ce.mmkv in your Android application, you should go to com.0ce.mmkV-static. Also, if your application relies on any third SDK embedded in com.tencent. MMKV, you can add the following lines to build.gradle to avoid conflicts:

    dependencies {
        ...

        modules {
            module("com.tencent:mmkv") {
                replacedBy("com.tencent:mmkv-static", "Using mmkv-static for flutter")
            }
        }
    }
Copy the code

use

import 'package:mmkv/mmkv.dart';

void main() async {

  // must wait for MMKV to finish initialization
  final rootDir = await MMKV.initialize();
  print('MMKV for flutter with rootDir = $rootDir');

  runApp(MyApp());
}


Copy the code
  import 'package:mmkv/mmkv.dart';

  var mmkv = MMKV.defaultMMKV();
  mmkv.encodeBool('bool', true);
  print('bool = ${mmkv.decodeBool('bool')}');

  mmkv.encodeInt32('int32', (1<<31) - 1);
  print('max int32 = ${mmkv.decodeInt32('int32')}');

  mmkv.encodeInt('int', (1<<63) - 1);
  print('max int = ${mmkv.decodeInt('int')}');

  String str = 'Hello Flutter from MMKV';
  mmkv.encodeString('string', str);
  print('string = ${mmkv.decodeString('string')}');

  str = 'Hello Flutter from MMKV with bytes';
  var bytes = MMBuffer.fromList(Utf8Encoder().convert(str));
  mmkv.encodeBytes('bytes', bytes);
  bytes.destroy();

  bytes = mmkv.decodeBytes('bytes');
  print('bytes = ${Utf8Decoder().convert(bytes.asList())}');
  bytes.destroy();
Copy the code

More ways to Use