Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

[shared_preferences] Migrate examples to NNBD #3564

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart=2.9

import 'dart:async';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ class MyApp extends StatelessWidget {
}

class SharedPreferencesDemo extends StatefulWidget {
SharedPreferencesDemo({Key key}) : super(key: key);
SharedPreferencesDemo({Key? key}) : super(key: key);

@override
SharedPreferencesDemoState createState() => SharedPreferencesDemoState();
}

class SharedPreferencesDemoState extends State<SharedPreferencesDemo> {
Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
Future<int> _counter;
Future<int>? _counter;

Future<void> _incrementCounter() async {
final SharedPreferences prefs = await _prefs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,5 @@ flutter:
uses-material-design: true

environment:
sdk: ">=2.0.0-dev.28.0 <3.0.0"
sdk: ">=2.12.0-0 <3.0.0"
flutter: ">=1.9.1+hotfix.2"

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// 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.

// @dart=2.9

import 'dart:async';
import 'dart:convert';
import 'dart:io';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart=2.9

import 'dart:async';

import 'package:flutter_test/flutter_test.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ class MyApp extends StatelessWidget {
}

class SharedPreferencesDemo extends StatefulWidget {
SharedPreferencesDemo({Key key}) : super(key: key);
SharedPreferencesDemo({Key? key}) : super(key: key);

@override
SharedPreferencesDemoState createState() => SharedPreferencesDemoState();
}

class SharedPreferencesDemoState extends State<SharedPreferencesDemo> {
final prefs = SharedPreferencesLinux.instance;
Future<int> _counter;
Future<int>? _counter;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nits: Maybe this should be late Future<int> _counter'?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in all examples. I think I was confusing myself by thinking of it as being initialized asynchronously so needing to be ?, forgetting that the value is async, but the future itself isn't.


Future<void> _incrementCounter() async {
final values = await prefs.getAll();
final int counter = (values['counter'] as int ?? 0) + 1;
final int counter = (values['counter'] as int? ?? 0) + 1;

setState(() {
_counter = prefs.setValue(null, "counter", counter).then((bool success) {
_counter = prefs.setValue('Int', 'counter', counter).then((bool success) {
return counter;
});
});
Expand All @@ -49,7 +49,7 @@ class SharedPreferencesDemoState extends State<SharedPreferencesDemo> {
void initState() {
super.initState();
_counter = prefs.getAll().then((Map<String, Object> values) {
return (values['counter'] ?? 0);
return (values['counter'] as int? ?? 0);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ flutter:
uses-material-design: true

environment:
sdk: ">=2.1.0 <3.0.0"
sdk: ">=2.12.0-0 <3.0.0"
flutter: ">=1.12.8"
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// 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.

// @dart=2.9

import 'dart:async';
import 'dart:convert';
import 'dart:io';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
// Copyright 2017, 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.

// @dart=2.9

import 'dart:async';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';
import 'package:integration_test/integration_test.dart';

void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

group('$SharedPreferences', () {
group('SharedPreferencesMacOS', () {
const Map<String, dynamic> kTestValues = <String, dynamic>{
'flutter.String': 'hello world',
'flutter.bool': true,
Expand All @@ -23,67 +29,81 @@ void main() {
'flutter.List': <String>['baz', 'quox'],
};

SharedPreferences preferences;
SharedPreferencesStorePlatform preferences;

setUp(() async {
preferences = await SharedPreferences.getInstance();
preferences = SharedPreferencesStorePlatform.instance;
});

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);
// Normally the app-facing package adds the prefix, but since this test
// bypasses the app-facing package it needs to be manually added.
String _prefixedKey(String key) {
return 'flutter.$key';
}

testWidgets('reading', (WidgetTester _) async {
final Map<String, Object> values = await preferences.getAll();
expect(values[_prefixedKey('String')], isNull);
expect(values[_prefixedKey('bool')], isNull);
expect(values[_prefixedKey('int')], isNull);
expect(values[_prefixedKey('double')], isNull);
expect(values[_prefixedKey('List')], isNull);
});

test('writing', () async {
testWidgets('writing', (WidgetTester _) 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'])
preferences.setValue(
'String', _prefixedKey('String'), kTestValues2['flutter.String']),
preferences.setValue(
'Bool', _prefixedKey('bool'), kTestValues2['flutter.bool']),
preferences.setValue(
'Int', _prefixedKey('int'), kTestValues2['flutter.int']),
preferences.setValue(
'Double', _prefixedKey('double'), kTestValues2['flutter.double']),
preferences.setValue(
'StringList', _prefixedKey('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']);
final Map<String, Object> values = await preferences.getAll();
expect(values[_prefixedKey('String')], kTestValues2['flutter.String']);
expect(values[_prefixedKey('bool')], kTestValues2['flutter.bool']);
expect(values[_prefixedKey('int')], kTestValues2['flutter.int']);
expect(values[_prefixedKey('double')], kTestValues2['flutter.double']);
expect(values[_prefixedKey('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']);
testWidgets('removing', (WidgetTester _) async {
final String key = _prefixedKey('testKey');
await preferences.setValue('String', key, kTestValues['flutter.String']);
await preferences.setValue('Bool', key, kTestValues['flutter.bool']);
await preferences.setValue('Int', key, kTestValues['flutter.int']);
await preferences.setValue('Double', key, kTestValues['flutter.double']);
await preferences.setValue(
'StringList', key, kTestValues['flutter.List']);
await preferences.remove(key);
expect(preferences.get('testKey'), isNull);
final Map<String, Object> values = await preferences.getAll();
expect(values[key], 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']);
testWidgets('clearing', (WidgetTester _) async {
await preferences.setValue(
'String', 'String', kTestValues['flutter.String']);
await preferences.setValue('Bool', 'bool', kTestValues['flutter.bool']);
await preferences.setValue('Int', 'int', kTestValues['flutter.int']);
await preferences.setValue(
'Double', 'double', kTestValues['flutter.double']);
await preferences.setValue(
'StringList', '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);
final Map<String, Object> values = await preferences.getAll();
expect(values['String'], null);
expect(values['bool'], null);
expect(values['int'], null);
expect(values['double'], null);
expect(values['List'], null);
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';

void main() {
runApp(MyApp());
Expand All @@ -24,22 +24,28 @@ class MyApp extends StatelessWidget {
}

class SharedPreferencesDemo extends StatefulWidget {
SharedPreferencesDemo({Key key}) : super(key: key);
SharedPreferencesDemo({Key? key}) : super(key: key);

@override
SharedPreferencesDemoState createState() => SharedPreferencesDemoState();
}

class SharedPreferencesDemoState extends State<SharedPreferencesDemo> {
Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
Future<int> _counter;
SharedPreferencesStorePlatform _prefs =
SharedPreferencesStorePlatform.instance;
late Future<int> _counter;

// Includes the prefix because this is using the platform interface directly,
// but the prefix (which the native code assumes is present) is added by the
// app-facing package.
static const String _prefKey = 'flutter.counter';

Future<void> _incrementCounter() async {
final SharedPreferences prefs = await _prefs;
final int counter = (prefs.getInt('counter') ?? 0) + 1;
final Map<String, Object> values = await _prefs.getAll();
final int counter = ((values[_prefKey] as int?) ?? 0) + 1;

setState(() {
_counter = prefs.setInt("counter", counter).then((bool success) {
_counter = _prefs.setValue('Int', _prefKey, counter).then((bool success) {
return counter;
});
});
Expand All @@ -48,8 +54,8 @@ class SharedPreferencesDemoState extends State<SharedPreferencesDemo> {
@override
void initState() {
super.initState();
_counter = _prefs.then((SharedPreferences prefs) {
return (prefs.getInt('counter') ?? 0);
_counter = _prefs.getAll().then((Map<String, Object> values) {
return (values[_prefKey] as int?) ?? 0;
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@
33CC10F32044A3C60003C045 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 33CC10F22044A3C60003C045 /* Assets.xcassets */; };
33CC10F62044A3C60003C045 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 33CC10F42044A3C60003C045 /* MainMenu.xib */; };
33CC11132044BFA00003C045 /* MainFlutterWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 33CC11122044BFA00003C045 /* MainFlutterWindow.swift */; };
33D1A10422148B71006C7A3E /* FlutterMacOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 33D1A10322148B71006C7A3E /* FlutterMacOS.framework */; };
33D1A10522148B93006C7A3E /* FlutterMacOS.framework in Bundle Framework */ = {isa = PBXBuildFile; fileRef = 33D1A10322148B71006C7A3E /* FlutterMacOS.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
D73912F022F37F9E000D13A0 /* App.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D73912EF22F37F9E000D13A0 /* App.framework */; };
D73912F222F3801D000D13A0 /* App.framework in Bundle Framework */ = {isa = PBXBuildFile; fileRef = D73912EF22F37F9E000D13A0 /* App.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
DD4A1B9DEDBB72C87CD7AE27 /* Pods_Runner.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5067D74CB28D28AE3B3DD05B /* Pods_Runner.framework */; };
/* End PBXBuildFile section */

Expand All @@ -50,8 +46,6 @@
dstPath = "";
dstSubfolderSpec = 10;
files = (
D73912F222F3801D000D13A0 /* App.framework in Bundle Framework */,
33D1A10522148B93006C7A3E /* FlutterMacOS.framework in Bundle Framework */,
);
name = "Bundle Framework";
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -70,7 +64,6 @@
33CEB47222A05771004F2AC0 /* Flutter-Debug.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Flutter-Debug.xcconfig"; sourceTree = "<group>"; };
33CEB47422A05771004F2AC0 /* Flutter-Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Flutter-Release.xcconfig"; sourceTree = "<group>"; };
33CEB47722A0578A004F2AC0 /* Flutter-Generated.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = "Flutter-Generated.xcconfig"; path = "ephemeral/Flutter-Generated.xcconfig"; sourceTree = "<group>"; };
33D1A10322148B71006C7A3E /* FlutterMacOS.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = FlutterMacOS.framework; path = Flutter/ephemeral/FlutterMacOS.framework; sourceTree = SOURCE_ROOT; };
33E51913231747F40026EE4D /* DebugProfile.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = DebugProfile.entitlements; sourceTree = "<group>"; };
33E51914231749380026EE4D /* Release.entitlements */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.entitlements; path = Release.entitlements; sourceTree = "<group>"; };
33E5194F232828860026EE4D /* AppInfo.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = AppInfo.xcconfig; sourceTree = "<group>"; };
Expand All @@ -80,16 +73,13 @@
899489AD6AA35AECA4E2BEA6 /* Pods-Runner.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.debug.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"; sourceTree = "<group>"; };
9740EEB21CF90195004384FC /* Debug.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = Debug.xcconfig; sourceTree = "<group>"; };
B36FDC1D769C9045B8821207 /* Pods-Runner.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.release.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"; sourceTree = "<group>"; };
D73912EF22F37F9E000D13A0 /* App.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = App.framework; path = Flutter/ephemeral/App.framework; sourceTree = SOURCE_ROOT; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
33CC10EA2044A3C60003C045 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
D73912F022F37F9E000D13A0 /* App.framework in Frameworks */,
33D1A10422148B71006C7A3E /* FlutterMacOS.framework in Frameworks */,
DD4A1B9DEDBB72C87CD7AE27 /* Pods_Runner.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -145,8 +135,6 @@
33CEB47222A05771004F2AC0 /* Flutter-Debug.xcconfig */,
33CEB47422A05771004F2AC0 /* Flutter-Release.xcconfig */,
33CEB47722A0578A004F2AC0 /* Flutter-Generated.xcconfig */,
D73912EF22F37F9E000D13A0 /* App.framework */,
33D1A10322148B71006C7A3E /* FlutterMacOS.framework */,
);
path = Flutter;
sourceTree = "<group>";
Expand Down Expand Up @@ -281,7 +269,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "echo \"$PRODUCT_NAME.app\" > \"$PROJECT_DIR\"/Flutter/ephemeral/.app_filename\n";
shellScript = "echo \"$PRODUCT_NAME.app\" > \"$PROJECT_DIR\"/Flutter/ephemeral/.app_filename && \"$FLUTTER_ROOT\"/packages/flutter_tools/bin/macos_assemble.sh embed\n";
};
33CC111E2044C6BF0003C045 /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
Expand All @@ -308,10 +296,13 @@
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
inputPaths = (
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/shared_preferences_macos/shared_preferences_macos.framework",
);
name = "[CP] Embed Pods Frameworks";
outputFileListPaths = (
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/shared_preferences_macos.framework",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading