[shared_preferences] Shared preferences linux endorsement (#2864)
Endorses shared_preferences_linux.
Since shared_preferences_linux is pure Dart, this uses manual registration of the Linux implementation. It is similar to the solution used for path_provider, but improves on it by more narrowly tailoring the registration to cases where another registration hasn't already been done, and only checking the first time, thus avoiding the need for a variable to override it.
Updates shared_preferences_linux to depend on path_provider_linux rather than path_provider, to avoid adding unnecessary native plugin dependencies to other platforms.
Also adds a driver test for shared_preference_linux, which I missed in reviewing the initial shared_preferences_linux landing.
diff --git a/packages/shared_preferences/shared_preferences/CHANGELOG.md b/packages/shared_preferences/shared_preferences/CHANGELOG.md
index 8c7ee24..d17bbb9 100644
--- a/packages/shared_preferences/shared_preferences/CHANGELOG.md
+++ b/packages/shared_preferences/shared_preferences/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 0.5.8
+
+* Support Linux by default.
+
## 0.5.7+3
* Post-v2 Android embedding cleanup.
diff --git a/packages/shared_preferences/shared_preferences/README.md b/packages/shared_preferences/shared_preferences/README.md
index b5bf405..9ccac0a 100644
--- a/packages/shared_preferences/shared_preferences/README.md
+++ b/packages/shared_preferences/shared_preferences/README.md
@@ -2,10 +2,10 @@
[![pub package](https://img.shields.io/pub/v/shared_preferences.svg)](https://pub.dartlang.org/packages/shared_preferences)
-Wraps NSUserDefaults (on iOS) and SharedPreferences (on Android), providing
-a persistent store for simple data. Data is persisted to disk asynchronously.
-Neither platform can guarantee that writes will be persisted to disk after
-returning and this plugin must not be used for storing critical data.
+Wraps platform-specific persistent storage for simple data
+(NSUserDefaults on iOS and macOS, SharedPreferences on Android, etc.). Data may be persisted to disk asynchronously,
+and there is no guarantee that writes will be persisted to disk after
+returning, so this plugin must not be used for storing critical data.
**Please set your constraint to `shared_preferences: '>=0.5.y+x <2.0.0'`**
diff --git a/packages/shared_preferences/shared_preferences/lib/shared_preferences.dart b/packages/shared_preferences/shared_preferences/lib/shared_preferences.dart
index 62160de..b8d3452 100644
--- a/packages/shared_preferences/shared_preferences/lib/shared_preferences.dart
+++ b/packages/shared_preferences/shared_preferences/lib/shared_preferences.dart
@@ -3,10 +3,14 @@
// found in the LICENSE file.
import 'dart:async';
+import 'dart:io' show Platform;
+import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:meta/meta.dart';
+import 'package:shared_preferences_linux/shared_preferences_linux.dart';
import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';
+import 'package:shared_preferences_platform_interface/method_channel_shared_preferences.dart';
/// Wraps NSUserDefaults (on iOS) and SharedPreferences (on Android), providing
/// a persistent store for simple data.
@@ -17,9 +21,26 @@
static const String _prefix = 'flutter.';
static Completer<SharedPreferences> _completer;
+ static bool _manualDartRegistrationNeeded = true;
- static SharedPreferencesStorePlatform get _store =>
- SharedPreferencesStorePlatform.instance;
+ static SharedPreferencesStorePlatform get _store {
+ // This is to manually endorse the Linux implementation until automatic
+ // registration of dart plugins is implemented. For details see
+ // https://github.com/flutter/flutter/issues/52267.
+ if (_manualDartRegistrationNeeded) {
+ // Only do the initial registration if it hasn't already been overridden
+ // with a non-default instance.
+ if (!kIsWeb &&
+ Platform.isLinux &&
+ SharedPreferencesStorePlatform.instance
+ is MethodChannelSharedPreferencesStore) {
+ SharedPreferencesStorePlatform.instance = SharedPreferencesLinux();
+ }
+ _manualDartRegistrationNeeded = false;
+ }
+
+ return SharedPreferencesStorePlatform.instance;
+ }
/// Loads and parses the [SharedPreferences] for this app from disk.
///
diff --git a/packages/shared_preferences/shared_preferences/pubspec.yaml b/packages/shared_preferences/shared_preferences/pubspec.yaml
index 2b27824..ca8c443 100644
--- a/packages/shared_preferences/shared_preferences/pubspec.yaml
+++ b/packages/shared_preferences/shared_preferences/pubspec.yaml
@@ -5,7 +5,7 @@
# 0.5.y+z is compatible with 1.0.0, if you land a breaking change bump
# the version to 2.0.0.
# See more details: https://github.com/flutter/flutter/wiki/Package-migration-to-1.0.0
-version: 0.5.7+3
+version: 0.5.8
flutter:
plugin:
@@ -15,6 +15,8 @@
pluginClass: SharedPreferencesPlugin
ios:
pluginClass: FLTSharedPreferencesPlugin
+ linux:
+ default_package: shared_preferences_linux
macos:
default_package: shared_preferences_macos
web:
@@ -30,6 +32,7 @@
# validation, so we set a ^ constraint.
# TODO(franciscojma): Revisit this (either update this part in the design or the pub tool).
# https://github.com/flutter/flutter/issues/46264
+ shared_preferences_linux: ^0.0.2
shared_preferences_macos: ^0.0.1
shared_preferences_web: ^0.1.2
diff --git a/packages/shared_preferences/shared_preferences_linux/CHANGELOG.md b/packages/shared_preferences/shared_preferences_linux/CHANGELOG.md
index de171a9..353a921 100644
--- a/packages/shared_preferences/shared_preferences_linux/CHANGELOG.md
+++ b/packages/shared_preferences/shared_preferences_linux/CHANGELOG.md
@@ -1,3 +1,6 @@
+## 0.0.2+1
+* Replace path_provider dependency with path_provider_linux.
+
## 0.0.2
* Add iOS stub.
diff --git a/packages/shared_preferences/shared_preferences_linux/example/linux/CMakeLists.txt b/packages/shared_preferences/shared_preferences_linux/example/linux/CMakeLists.txt
new file mode 100644
index 0000000..0236a88
--- /dev/null
+++ b/packages/shared_preferences/shared_preferences_linux/example/linux/CMakeLists.txt
@@ -0,0 +1,95 @@
+cmake_minimum_required(VERSION 3.10)
+project(runner LANGUAGES CXX)
+
+set(BINARY_NAME "example")
+
+cmake_policy(SET CMP0063 NEW)
+
+set(CMAKE_INSTALL_RPATH "$ORIGIN/lib")
+
+# Configure build options.
+if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
+ set(CMAKE_BUILD_TYPE "Debug" CACHE
+ STRING "Flutter build mode" FORCE)
+ set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
+ "Debug" "Profile" "Release")
+endif()
+
+# Compilation settings that should be applied to most targets.
+function(APPLY_STANDARD_SETTINGS TARGET)
+ target_compile_features(${TARGET} PUBLIC cxx_std_14)
+ target_compile_options(${TARGET} PRIVATE -Wall -Werror)
+ target_compile_options(${TARGET} PRIVATE "$<$<NOT:$<CONFIG:Debug>>:-O3>")
+ target_compile_definitions(${TARGET} PRIVATE "$<$<NOT:$<CONFIG:Debug>>:NDEBUG>")
+endfunction()
+
+set(FLUTTER_MANAGED_DIR "${CMAKE_CURRENT_SOURCE_DIR}/flutter")
+
+# Flutter library and tool build rules.
+add_subdirectory(${FLUTTER_MANAGED_DIR})
+
+# System-level dependencies.
+find_package(PkgConfig REQUIRED)
+pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0)
+
+# Application build
+add_executable(${BINARY_NAME}
+ "main.cc"
+ "my_application.cc"
+ "${FLUTTER_MANAGED_DIR}/generated_plugin_registrant.cc"
+)
+apply_standard_settings(${BINARY_NAME})
+target_link_libraries(${BINARY_NAME} PRIVATE flutter)
+target_link_libraries(${BINARY_NAME} PRIVATE PkgConfig::GTK)
+add_dependencies(${BINARY_NAME} flutter_assemble)
+
+# Generated plugin build rules, which manage building the plugins and adding
+# them to the application.
+include(flutter/generated_plugins.cmake)
+
+
+# === Installation ===
+# By default, "installing" just makes a relocatable bundle in the build
+# directory.
+set(BUILD_BUNDLE_DIR "${PROJECT_BINARY_DIR}/bundle")
+if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
+ set(CMAKE_INSTALL_PREFIX "${BUILD_BUNDLE_DIR}" CACHE PATH "..." FORCE)
+endif()
+
+# Start with a clean build bundle directory every time.
+install(CODE "
+ file(REMOVE_RECURSE \"${BUILD_BUNDLE_DIR}/\")
+ " COMPONENT Runtime)
+
+set(INSTALL_BUNDLE_DATA_DIR "${CMAKE_INSTALL_PREFIX}/data")
+set(INSTALL_BUNDLE_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib")
+
+install(TARGETS ${BINARY_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}"
+ COMPONENT Runtime)
+
+install(FILES "${FLUTTER_ICU_DATA_FILE}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}"
+ COMPONENT Runtime)
+
+install(FILES "${FLUTTER_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
+ COMPONENT Runtime)
+
+if(PLUGIN_BUNDLED_LIBRARIES)
+ install(FILES "${PLUGIN_BUNDLED_LIBRARIES}"
+ DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
+ COMPONENT Runtime)
+endif()
+
+# Fully re-copy the assets directory on each build to avoid having stale files
+# from a previous install.
+set(FLUTTER_ASSET_DIR_NAME "flutter_assets")
+install(CODE "
+ file(REMOVE_RECURSE \"${INSTALL_BUNDLE_DATA_DIR}/${FLUTTER_ASSET_DIR_NAME}\")
+ " COMPONENT Runtime)
+install(DIRECTORY "${PROJECT_BUILD_DIR}/${FLUTTER_ASSET_DIR_NAME}"
+ DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" COMPONENT Runtime)
+
+# Install the AOT library on non-Debug builds only.
+if(NOT CMAKE_BUILD_TYPE MATCHES "Debug")
+ install(FILES "${AOT_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
+ COMPONENT Runtime)
+endif()
diff --git a/packages/shared_preferences/shared_preferences_linux/example/linux/flutter/CMakeLists.txt b/packages/shared_preferences/shared_preferences_linux/example/linux/flutter/CMakeLists.txt
new file mode 100644
index 0000000..94f43ff
--- /dev/null
+++ b/packages/shared_preferences/shared_preferences_linux/example/linux/flutter/CMakeLists.txt
@@ -0,0 +1,86 @@
+cmake_minimum_required(VERSION 3.10)
+
+set(EPHEMERAL_DIR "${CMAKE_CURRENT_SOURCE_DIR}/ephemeral")
+
+# Configuration provided via flutter tool.
+include(${EPHEMERAL_DIR}/generated_config.cmake)
+
+# TODO: Move the rest of this into files in ephemeral. See
+# https://github.com/flutter/flutter/issues/57146.
+
+# Serves the same purpose as list(TRANSFORM ... PREPEND ...),
+# which isn't available in 3.10.
+function(list_prepend LIST_NAME PREFIX)
+ set(NEW_LIST "")
+ foreach(element ${${LIST_NAME}})
+ list(APPEND NEW_LIST "${PREFIX}${element}")
+ endforeach(element)
+ set(${LIST_NAME} "${NEW_LIST}" PARENT_SCOPE)
+endfunction()
+
+# === Flutter Library ===
+# System-level dependencies.
+find_package(PkgConfig REQUIRED)
+pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0)
+pkg_check_modules(GLIB REQUIRED IMPORTED_TARGET glib-2.0)
+pkg_check_modules(GIO REQUIRED IMPORTED_TARGET gio-2.0)
+
+set(FLUTTER_LIBRARY "${EPHEMERAL_DIR}/libflutter_linux_gtk.so")
+
+# Published to parent scope for install step.
+set(FLUTTER_LIBRARY ${FLUTTER_LIBRARY} PARENT_SCOPE)
+set(FLUTTER_ICU_DATA_FILE "${EPHEMERAL_DIR}/icudtl.dat" PARENT_SCOPE)
+set(PROJECT_BUILD_DIR "${PROJECT_DIR}/build/" PARENT_SCOPE)
+set(AOT_LIBRARY "${PROJECT_DIR}/build/lib/libapp.so" PARENT_SCOPE)
+
+list(APPEND FLUTTER_LIBRARY_HEADERS
+ "fl_basic_message_channel.h"
+ "fl_binary_codec.h"
+ "fl_binary_messenger.h"
+ "fl_dart_project.h"
+ "fl_engine.h"
+ "fl_json_message_codec.h"
+ "fl_json_method_codec.h"
+ "fl_message_codec.h"
+ "fl_method_call.h"
+ "fl_method_channel.h"
+ "fl_method_codec.h"
+ "fl_method_response.h"
+ "fl_plugin_registrar.h"
+ "fl_plugin_registry.h"
+ "fl_standard_message_codec.h"
+ "fl_standard_method_codec.h"
+ "fl_string_codec.h"
+ "fl_value.h"
+ "fl_view.h"
+ "flutter_linux.h"
+)
+list_prepend(FLUTTER_LIBRARY_HEADERS "${EPHEMERAL_DIR}/flutter_linux/")
+add_library(flutter INTERFACE)
+target_include_directories(flutter INTERFACE
+ "${EPHEMERAL_DIR}"
+)
+target_link_libraries(flutter INTERFACE "${FLUTTER_LIBRARY}")
+target_link_libraries(flutter INTERFACE
+ PkgConfig::GTK
+ PkgConfig::GLIB
+ PkgConfig::GIO
+)
+add_dependencies(flutter flutter_assemble)
+
+# === Flutter tool backend ===
+# _phony_ is a non-existent file to force this command to run every time,
+# since currently there's no way to get a full input/output list from the
+# flutter tool.
+add_custom_command(
+ OUTPUT ${FLUTTER_LIBRARY} ${FLUTTER_LIBRARY_HEADERS}
+ ${CMAKE_CURRENT_BINARY_DIR}/_phony_
+ COMMAND ${CMAKE_COMMAND} -E env
+ ${FLUTTER_TOOL_ENVIRONMENT}
+ "${FLUTTER_ROOT}/packages/flutter_tools/bin/tool_backend.sh"
+ linux-x64 ${CMAKE_BUILD_TYPE}
+)
+add_custom_target(flutter_assemble DEPENDS
+ "${FLUTTER_LIBRARY}"
+ ${FLUTTER_LIBRARY_HEADERS}
+)
diff --git a/packages/shared_preferences/shared_preferences_linux/example/linux/flutter/generated_plugin_registrant.cc b/packages/shared_preferences/shared_preferences_linux/example/linux/flutter/generated_plugin_registrant.cc
new file mode 100644
index 0000000..d38195a
--- /dev/null
+++ b/packages/shared_preferences/shared_preferences_linux/example/linux/flutter/generated_plugin_registrant.cc
@@ -0,0 +1,9 @@
+//
+// Generated file. Do not edit.
+//
+
+#include "generated_plugin_registrant.h"
+
+
+void fl_register_plugins(FlPluginRegistry* registry) {
+}
diff --git a/packages/shared_preferences/shared_preferences_linux/example/linux/flutter/generated_plugin_registrant.h b/packages/shared_preferences/shared_preferences_linux/example/linux/flutter/generated_plugin_registrant.h
new file mode 100644
index 0000000..9bf7478
--- /dev/null
+++ b/packages/shared_preferences/shared_preferences_linux/example/linux/flutter/generated_plugin_registrant.h
@@ -0,0 +1,13 @@
+//
+// Generated file. Do not edit.
+//
+
+#ifndef GENERATED_PLUGIN_REGISTRANT_
+#define GENERATED_PLUGIN_REGISTRANT_
+
+#include <flutter_linux/flutter_linux.h>
+
+// Registers Flutter plugins.
+void fl_register_plugins(FlPluginRegistry* registry);
+
+#endif // GENERATED_PLUGIN_REGISTRANT_
diff --git a/packages/shared_preferences/shared_preferences_linux/example/linux/flutter/generated_plugins.cmake b/packages/shared_preferences/shared_preferences_linux/example/linux/flutter/generated_plugins.cmake
new file mode 100644
index 0000000..51436ae
--- /dev/null
+++ b/packages/shared_preferences/shared_preferences_linux/example/linux/flutter/generated_plugins.cmake
@@ -0,0 +1,15 @@
+#
+# Generated file, do not edit.
+#
+
+list(APPEND FLUTTER_PLUGIN_LIST
+)
+
+set(PLUGIN_BUNDLED_LIBRARIES)
+
+foreach(plugin ${FLUTTER_PLUGIN_LIST})
+ add_subdirectory(flutter/ephemeral/.plugin_symlinks/${plugin}/linux plugins/${plugin})
+ target_link_libraries(${BINARY_NAME} PRIVATE ${plugin}_plugin)
+ list(APPEND PLUGIN_BUNDLED_LIBRARIES $<TARGET_FILE:${plugin}_plugin>)
+ list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${plugin}_bundled_libraries})
+endforeach(plugin)
diff --git a/packages/shared_preferences/shared_preferences_linux/example/linux/main.cc b/packages/shared_preferences/shared_preferences_linux/example/linux/main.cc
new file mode 100644
index 0000000..e7c5c54
--- /dev/null
+++ b/packages/shared_preferences/shared_preferences_linux/example/linux/main.cc
@@ -0,0 +1,6 @@
+#include "my_application.h"
+
+int main(int argc, char** argv) {
+ g_autoptr(MyApplication) app = my_application_new();
+ return g_application_run(G_APPLICATION(app), argc, argv);
+}
diff --git a/packages/shared_preferences/shared_preferences_linux/example/linux/my_application.cc b/packages/shared_preferences/shared_preferences_linux/example/linux/my_application.cc
new file mode 100644
index 0000000..c2357f1
--- /dev/null
+++ b/packages/shared_preferences/shared_preferences_linux/example/linux/my_application.cc
@@ -0,0 +1,44 @@
+#include "my_application.h"
+
+#include <flutter_linux/flutter_linux.h>
+
+#include "flutter/generated_plugin_registrant.h"
+
+struct _MyApplication {
+ GtkApplication parent_instance;
+};
+
+G_DEFINE_TYPE(MyApplication, my_application, GTK_TYPE_APPLICATION)
+
+// Implements GApplication::activate.
+static void my_application_activate(GApplication* application) {
+ GtkWindow* window =
+ GTK_WINDOW(gtk_application_window_new(GTK_APPLICATION(application)));
+ GtkHeaderBar *header_bar = GTK_HEADER_BAR(gtk_header_bar_new());
+ gtk_widget_show(GTK_WIDGET(header_bar));
+ gtk_header_bar_set_title(header_bar, "example");
+ gtk_header_bar_set_show_close_button(header_bar, TRUE);
+ gtk_window_set_titlebar(window, GTK_WIDGET(header_bar));
+ gtk_window_set_default_size(window, 1280, 720);
+ gtk_widget_show(GTK_WIDGET(window));
+
+ g_autoptr(FlDartProject) project = fl_dart_project_new();
+
+ FlView* view = fl_view_new(project);
+ gtk_widget_show(GTK_WIDGET(view));
+ gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(view));
+
+ fl_register_plugins(FL_PLUGIN_REGISTRY(view));
+
+ gtk_widget_grab_focus(GTK_WIDGET(view));
+}
+
+static void my_application_class_init(MyApplicationClass* klass) {
+ G_APPLICATION_CLASS(klass)->activate = my_application_activate;
+}
+
+static void my_application_init(MyApplication* self) {}
+
+MyApplication* my_application_new() {
+ return MY_APPLICATION(g_object_new(my_application_get_type(), nullptr));
+}
diff --git a/packages/shared_preferences/shared_preferences_linux/example/linux/my_application.h b/packages/shared_preferences/shared_preferences_linux/example/linux/my_application.h
new file mode 100644
index 0000000..72271d5
--- /dev/null
+++ b/packages/shared_preferences/shared_preferences_linux/example/linux/my_application.h
@@ -0,0 +1,18 @@
+#ifndef FLUTTER_MY_APPLICATION_H_
+#define FLUTTER_MY_APPLICATION_H_
+
+#include <gtk/gtk.h>
+
+G_DECLARE_FINAL_TYPE(MyApplication, my_application, MY, APPLICATION,
+ GtkApplication)
+
+/**
+ * my_application_new:
+ *
+ * Creates a new Flutter-based application.
+ *
+ * Returns: a new #MyApplication.
+ */
+MyApplication* my_application_new();
+
+#endif // FLUTTER_MY_APPLICATION_H_
diff --git a/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml b/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml
index 1c06240..8e726d6 100644
--- a/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml
+++ b/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml
@@ -4,11 +4,22 @@
dependencies:
flutter:
sdk: flutter
+ shared_preferences: any
shared_preferences_linux: ^0.1.0
dependency_overrides:
shared_preferences_linux:
path: ../
+ # Remove this override once the endorsement is published.
+ shared_preferences:
+ path: ../../shared_preferences/
+
+dev_dependencies:
+ flutter_driver:
+ sdk: flutter
+ test: any
+ e2e: ^0.2.0
+ pedantic: ^1.8.0
flutter:
uses-material-design: true
diff --git a/packages/shared_preferences/shared_preferences_linux/example/test_driver/shared_preferences_e2e.dart b/packages/shared_preferences/shared_preferences_linux/example/test_driver/shared_preferences_e2e.dart
new file mode 100644
index 0000000..b693df2
--- /dev/null
+++ b/packages/shared_preferences/shared_preferences_linux/example/test_driver/shared_preferences_e2e.dart
@@ -0,0 +1,89 @@
+import 'dart:async';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:shared_preferences/shared_preferences.dart';
+import 'package:e2e/e2e.dart';
+
+void main() {
+ E2EWidgetsFlutterBinding.ensureInitialized();
+
+ group('$SharedPreferences', () {
+ const Map<String, dynamic> kTestValues = <String, dynamic>{
+ 'flutter.String': 'hello world',
+ 'flutter.bool': true,
+ 'flutter.int': 42,
+ 'flutter.double': 3.14159,
+ 'flutter.List': <String>['foo', 'bar'],
+ };
+
+ const Map<String, dynamic> kTestValues2 = <String, dynamic>{
+ 'flutter.String': 'goodbye world',
+ 'flutter.bool': false,
+ 'flutter.int': 1337,
+ 'flutter.double': 2.71828,
+ 'flutter.List': <String>['baz', 'quox'],
+ };
+
+ SharedPreferences preferences;
+
+ setUp(() async {
+ preferences = await SharedPreferences.getInstance();
+ });
+
+ tearDown(() {
+ preferences.clear();
+ });
+
+ test('reading', () async {
+ expect(preferences.get('String'), isNull);
+ expect(preferences.get('bool'), isNull);
+ expect(preferences.get('int'), isNull);
+ expect(preferences.get('double'), isNull);
+ expect(preferences.get('List'), isNull);
+ expect(preferences.getString('String'), isNull);
+ expect(preferences.getBool('bool'), isNull);
+ expect(preferences.getInt('int'), isNull);
+ expect(preferences.getDouble('double'), isNull);
+ expect(preferences.getStringList('List'), isNull);
+ });
+
+ test('writing', () async {
+ await Future.wait(<Future<bool>>[
+ preferences.setString('String', kTestValues2['flutter.String']),
+ preferences.setBool('bool', kTestValues2['flutter.bool']),
+ preferences.setInt('int', kTestValues2['flutter.int']),
+ preferences.setDouble('double', kTestValues2['flutter.double']),
+ preferences.setStringList('List', kTestValues2['flutter.List'])
+ ]);
+ expect(preferences.getString('String'), kTestValues2['flutter.String']);
+ expect(preferences.getBool('bool'), kTestValues2['flutter.bool']);
+ expect(preferences.getInt('int'), kTestValues2['flutter.int']);
+ expect(preferences.getDouble('double'), kTestValues2['flutter.double']);
+ expect(preferences.getStringList('List'), kTestValues2['flutter.List']);
+ });
+
+ test('removing', () async {
+ const String key = 'testKey';
+ await preferences.setString(key, kTestValues['flutter.String']);
+ await preferences.setBool(key, kTestValues['flutter.bool']);
+ await preferences.setInt(key, kTestValues['flutter.int']);
+ await preferences.setDouble(key, kTestValues['flutter.double']);
+ await preferences.setStringList(key, kTestValues['flutter.List']);
+ await preferences.remove(key);
+ expect(preferences.get('testKey'), isNull);
+ });
+
+ test('clearing', () async {
+ await preferences.setString('String', kTestValues['flutter.String']);
+ await preferences.setBool('bool', kTestValues['flutter.bool']);
+ await preferences.setInt('int', kTestValues['flutter.int']);
+ await preferences.setDouble('double', kTestValues['flutter.double']);
+ await preferences.setStringList('List', kTestValues['flutter.List']);
+ await preferences.clear();
+ expect(preferences.getString('String'), null);
+ expect(preferences.getBool('bool'), null);
+ expect(preferences.getInt('int'), null);
+ expect(preferences.getDouble('double'), null);
+ expect(preferences.getStringList('List'), null);
+ });
+ });
+}
diff --git a/packages/shared_preferences/shared_preferences_linux/example/test_driver/shared_preferences_e2e_test.dart b/packages/shared_preferences/shared_preferences_linux/example/test_driver/shared_preferences_e2e_test.dart
new file mode 100644
index 0000000..f3aa9e2
--- /dev/null
+++ b/packages/shared_preferences/shared_preferences_linux/example/test_driver/shared_preferences_e2e_test.dart
@@ -0,0 +1,15 @@
+// Copyright 2019, the Chromium project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'dart:async';
+import 'dart:io';
+import 'package:flutter_driver/flutter_driver.dart';
+
+Future<void> main() async {
+ final FlutterDriver driver = await FlutterDriver.connect();
+ final String result =
+ await driver.requestData(null, timeout: const Duration(minutes: 1));
+ await driver.close();
+ exit(result == 'pass' ? 0 : 1);
+}
diff --git a/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart b/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart
index dc93100..c975ad1 100644
--- a/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart
+++ b/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart
@@ -4,11 +4,12 @@
import 'dart:async';
import 'dart:convert' show json;
+
import 'package:file/file.dart';
import 'package:file/local.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as path;
-import 'package:path_provider/path_provider.dart';
+import 'package:path_provider_linux/path_provider_linux.dart';
import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';
/// The Linux implementation of [SharedPreferencesStorePlatform].
@@ -27,9 +28,9 @@
/// Gets the file where the preferences are stored.
Future<File> _getLocalDataFile() async {
- var directory = await getApplicationSupportDirectory();
- var filePath = path.join(directory.path, 'shared_preferences.json');
- return fs.file(filePath);
+ final pathProvider = PathProviderLinux();
+ final directory = await pathProvider.getApplicationSupportPath();
+ return fs.file(path.join(directory, 'shared_preferences.json'));
}
/// Gets the preferences from the stored file. Once read, the preferences are
diff --git a/packages/shared_preferences/shared_preferences_linux/pubspec.yaml b/packages/shared_preferences/shared_preferences_linux/pubspec.yaml
index 95c3aca..3d40d39 100644
--- a/packages/shared_preferences/shared_preferences_linux/pubspec.yaml
+++ b/packages/shared_preferences/shared_preferences_linux/pubspec.yaml
@@ -1,6 +1,6 @@
name: shared_preferences_linux
description: Linux implementation of the shared_preferences plugin
-version: 0.0.2
+version: 0.0.2+1
homepage: https://github.com/flutter/plugins/tree/master/packages/shared_preferences/shared_preferences_linux
flutter:
@@ -20,7 +20,7 @@
sdk: flutter
meta: ^1.0.4
path: ^1.6.4
- path_provider: ^1.6.11
+ path_provider_linux: ^0.0.1
shared_preferences_platform_interface: ^1.0.0
dev_dependencies:
diff --git a/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart b/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart
index 8a794b1..8c659f2 100644
--- a/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart
+++ b/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart
@@ -4,19 +4,22 @@
import 'package:file/memory.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:path/path.dart' as path;
-import 'package:path_provider/path_provider.dart';
+import 'package:path_provider_linux/path_provider_linux.dart';
import 'package:shared_preferences_linux/shared_preferences_linux.dart';
-MemoryFileSystem fs = MemoryFileSystem.test();
+MemoryFileSystem fs;
void main() {
- setUp(() {});
+ setUp(() {
+ fs = MemoryFileSystem.test();
+ });
tearDown(() {});
Future<String> _getFilePath() async {
- var directory = await getApplicationSupportDirectory();
- return path.join(directory.path, 'shared_preferences.json');
+ final pathProvider = PathProviderLinux();
+ final directory = await pathProvider.getApplicationSupportPath();
+ return path.join(directory, 'shared_preferences.json');
}
_writeTestFile(String value) async {