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

Commit f17376f

Browse files
[shared_preferences] Add shared_preferences_windows (#2988)
Windows implementation of the shared_preferences plugin, using Dart+FFI.
1 parent a83f684 commit f17376f

File tree

16 files changed

+629
-0
lines changed

16 files changed

+629
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This file tracks properties of this Flutter project.
2+
# Used by Flutter tool to assess capabilities and perform upgrades etc.
3+
#
4+
# This file should be version controlled and should not be manually edited.
5+
6+
version:
7+
revision: df90bb5fd64e2066594151b9e311d45cd687a80c
8+
channel: master
9+
10+
project_type: plugin
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 0.0.1
2+
3+
* Initial release to support shared_preferences on Windows.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2017 The Chromium Authors. All rights reserved.
2+
//
3+
// Redistribution and use in source and binary forms, with or without
4+
// modification, are permitted provided that the following conditions are
5+
// met:
6+
//
7+
// * Redistributions of source code must retain the above copyright
8+
// notice, this list of conditions and the following disclaimer.
9+
// * Redistributions in binary form must reproduce the above
10+
// copyright notice, this list of conditions and the following disclaimer
11+
// in the documentation and/or other materials provided with the
12+
// distribution.
13+
// * Neither the name of Google Inc. nor the names of its
14+
// contributors may be used to endorse or promote products derived from
15+
// this software without specific prior written permission.
16+
//
17+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# shared_preferences_windows
2+
3+
The Windows implementation of [`shared_preferences`][1].
4+
5+
## Usage
6+
7+
### Import the package
8+
9+
This package has been endorsed, meaning that you only need to add `shared_preferences`
10+
as a dependency in your `pubspec.yaml`. It will be automatically included in your app
11+
when you depend on `package:shared_preferences`.
12+
13+
This is what the above means to your `pubspec.yaml`:
14+
15+
```yaml
16+
...
17+
dependencies:
18+
...
19+
shared_preferences: ^0.5.7
20+
...
21+
```
22+
23+
[1]: ../
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Miscellaneous
2+
*.class
3+
*.log
4+
*.pyc
5+
*.swp
6+
.DS_Store
7+
.atom/
8+
.buildlog/
9+
.history
10+
.svn/
11+
12+
# IntelliJ related
13+
*.iml
14+
*.ipr
15+
*.iws
16+
.idea/
17+
18+
# The .vscode folder contains launch configuration and tasks you configure in
19+
# VS Code which you may wish to be included in version control, so this line
20+
# is commented out by default.
21+
#.vscode/
22+
23+
# Flutter/Dart/Pub related
24+
**/doc/api/
25+
.dart_tool/
26+
.flutter-plugins
27+
.flutter-plugins-dependencies
28+
.packages
29+
.pub-cache/
30+
.pub/
31+
/build/
32+
33+
# Web related
34+
lib/generated_plugin_registrant.dart
35+
36+
# Symbolication related
37+
app.*.symbols
38+
39+
# Obfuscation related
40+
app.*.map.json
41+
42+
# Exceptions to above rules.
43+
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This file tracks properties of this Flutter project.
2+
# Used by Flutter tool to assess capabilities and perform upgrades etc.
3+
#
4+
# This file should be version controlled and should not be manually edited.
5+
6+
version:
7+
revision: df90bb5fd64e2066594151b9e311d45cd687a80c
8+
channel: master
9+
10+
project_type: app
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 0.0.1
2+
3+
* TODO: Describe initial release.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2017 The Chromium Authors. All rights reserved.
2+
//
3+
// Redistribution and use in source and binary forms, with or without
4+
// modification, are permitted provided that the following conditions are
5+
// met:
6+
//
7+
// * Redistributions of source code must retain the above copyright
8+
// notice, this list of conditions and the following disclaimer.
9+
// * Redistributions in binary form must reproduce the above
10+
// copyright notice, this list of conditions and the following disclaimer
11+
// in the documentation and/or other materials provided with the
12+
// distribution.
13+
// * Neither the name of Google Inc. nor the names of its
14+
// contributors may be used to endorse or promote products derived from
15+
// this software without specific prior written permission.
16+
//
17+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# shared_preferences_windows_example
2+
3+
Demonstrates how to use the shared_preferences_windows plugin.
4+
5+
## Getting Started
6+
7+
This project is a starting point for a Flutter application.
8+
9+
A few resources to get you started if this is your first Flutter project:
10+
11+
- [Lab: Write your first Flutter app](https://flutter.dev/docs/get-started/codelab)
12+
- [Cookbook: Useful Flutter samples](https://flutter.dev/docs/cookbook)
13+
14+
For help getting started with Flutter, view our
15+
[online documentation](https://flutter.dev/docs), which offers tutorials,
16+
samples, guidance on mobile development, and a full API reference.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright 2017 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
// ignore_for_file: public_member_api_docs
6+
7+
import 'dart:async';
8+
9+
import 'package:flutter/material.dart';
10+
import 'package:shared_preferences_windows/shared_preferences_windows.dart';
11+
12+
void main() {
13+
runApp(MyApp());
14+
}
15+
16+
class MyApp extends StatelessWidget {
17+
@override
18+
Widget build(BuildContext context) {
19+
return MaterialApp(
20+
title: 'SharedPreferences Demo',
21+
home: SharedPreferencesDemo(),
22+
);
23+
}
24+
}
25+
26+
class SharedPreferencesDemo extends StatefulWidget {
27+
SharedPreferencesDemo({Key key}) : super(key: key);
28+
29+
@override
30+
SharedPreferencesDemoState createState() => SharedPreferencesDemoState();
31+
}
32+
33+
class SharedPreferencesDemoState extends State<SharedPreferencesDemo> {
34+
final prefs = SharedPreferencesWindows.instance;
35+
Future<int> _counter;
36+
37+
Future<void> _incrementCounter() async {
38+
final values = await prefs.getAll();
39+
final int counter = (values['counter'] as int ?? 0) + 1;
40+
41+
setState(() {
42+
_counter = prefs.setValue(null, "counter", counter).then((bool success) {
43+
return counter;
44+
});
45+
});
46+
}
47+
48+
@override
49+
void initState() {
50+
super.initState();
51+
_counter = prefs.getAll().then((Map<String, Object> values) {
52+
return (values['counter'] ?? 0);
53+
});
54+
}
55+
56+
@override
57+
Widget build(BuildContext context) {
58+
return Scaffold(
59+
appBar: AppBar(
60+
title: const Text("SharedPreferences Demo"),
61+
),
62+
body: Center(
63+
child: FutureBuilder<int>(
64+
future: _counter,
65+
builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
66+
switch (snapshot.connectionState) {
67+
case ConnectionState.waiting:
68+
return const CircularProgressIndicator();
69+
default:
70+
if (snapshot.hasError) {
71+
return Text('Error: ${snapshot.error}');
72+
} else {
73+
return Text(
74+
'Button tapped ${snapshot.data} time${snapshot.data == 1 ? '' : 's'}.\n\n'
75+
'This should persist across restarts.',
76+
);
77+
}
78+
}
79+
})),
80+
floatingActionButton: FloatingActionButton(
81+
onPressed: _incrementCounter,
82+
tooltip: 'Increment',
83+
child: const Icon(Icons.add),
84+
),
85+
);
86+
}
87+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: shared_preferences_windows_example
2+
description: Demonstrates how to use the shared_preferences_windows plugin.
3+
4+
environment:
5+
sdk: ">=2.7.0 <3.0.0"
6+
7+
dependencies:
8+
flutter:
9+
sdk: flutter
10+
shared_preferences_windows: ^0.0.1
11+
12+
dependency_overrides:
13+
shared_preferences_windows:
14+
path: ../
15+
16+
dev_dependencies:
17+
flutter_driver:
18+
sdk: flutter
19+
test: any
20+
e2e: ^0.2.0
21+
pedantic: ^1.8.0
22+
23+
flutter:
24+
uses-material-design: true
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright 2017, the Chromium project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:async';
6+
import 'package:flutter_test/flutter_test.dart';
7+
import 'package:shared_preferences_windows/shared_preferences_windows.dart';
8+
import 'package:e2e/e2e.dart';
9+
10+
void main() {
11+
E2EWidgetsFlutterBinding.ensureInitialized();
12+
13+
group('SharedPreferencesWindows', () {
14+
const Map<String, dynamic> kTestValues = <String, dynamic>{
15+
'flutter.String': 'hello world',
16+
'flutter.bool': true,
17+
'flutter.int': 42,
18+
'flutter.double': 3.14159,
19+
'flutter.List': <String>['foo', 'bar'],
20+
};
21+
22+
const Map<String, dynamic> kTestValues2 = <String, dynamic>{
23+
'flutter.String': 'goodbye world',
24+
'flutter.bool': false,
25+
'flutter.int': 1337,
26+
'flutter.double': 2.71828,
27+
'flutter.List': <String>['baz', 'quox'],
28+
};
29+
30+
SharedPreferencesWindows preferences;
31+
32+
setUp(() async {
33+
preferences = SharedPreferencesWindows.instance;
34+
});
35+
36+
tearDown(() {
37+
preferences.clear();
38+
});
39+
40+
test('reading', () async {
41+
final Map<String, Object> values = await preferences.getAll();
42+
expect(values['String'], isNull);
43+
expect(values['bool'], isNull);
44+
expect(values['int'], isNull);
45+
expect(values['double'], isNull);
46+
expect(values['List'], isNull);
47+
});
48+
49+
test('writing', () async {
50+
await Future.wait(<Future<bool>>[
51+
preferences.setValue(
52+
'String', 'String', kTestValues2['flutter.String']),
53+
preferences.setValue('Bool', 'bool', kTestValues2['flutter.bool']),
54+
preferences.setValue('Int', 'int', kTestValues2['flutter.int']),
55+
preferences.setValue(
56+
'Double', 'double', kTestValues2['flutter.double']),
57+
preferences.setValue('StringList', 'List', kTestValues2['flutter.List'])
58+
]);
59+
final Map<String, Object> values = await preferences.getAll();
60+
expect(values['String'], kTestValues2['flutter.String']);
61+
expect(values['bool'], kTestValues2['flutter.bool']);
62+
expect(values['int'], kTestValues2['flutter.int']);
63+
expect(values['double'], kTestValues2['flutter.double']);
64+
expect(values['List'], kTestValues2['flutter.List']);
65+
});
66+
67+
test('removing', () async {
68+
const String key = 'testKey';
69+
await preferences.setValue('String', key, kTestValues['flutter.String']);
70+
await preferences.setValue('Bool', key, kTestValues['flutter.bool']);
71+
await preferences.setValue('Int', key, kTestValues['flutter.int']);
72+
await preferences.setValue('Double', key, kTestValues['flutter.double']);
73+
await preferences.setValue(
74+
'StringList', key, kTestValues['flutter.List']);
75+
await preferences.remove(key);
76+
final Map<String, Object> values = await preferences.getAll();
77+
expect(values[key], isNull);
78+
});
79+
80+
test('clearing', () async {
81+
await preferences.setValue(
82+
'String', 'String', kTestValues['flutter.String']);
83+
await preferences.setValue('Bool', 'bool', kTestValues['flutter.bool']);
84+
await preferences.setValue('Int', 'int', kTestValues['flutter.int']);
85+
await preferences.setValue(
86+
'Double', 'double', kTestValues['flutter.double']);
87+
await preferences.setValue(
88+
'StringList', 'List', kTestValues['flutter.List']);
89+
await preferences.clear();
90+
final Map<String, Object> values = await preferences.getAll();
91+
expect(values['String'], null);
92+
expect(values['bool'], null);
93+
expect(values['int'], null);
94+
expect(values['double'], null);
95+
expect(values['List'], null);
96+
});
97+
});
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2017, the Chromium project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:async';
6+
import 'dart:io';
7+
import 'package:flutter_driver/flutter_driver.dart';
8+
9+
Future<void> main() async {
10+
final FlutterDriver driver = await FlutterDriver.connect();
11+
final String result =
12+
await driver.requestData(null, timeout: const Duration(minutes: 1));
13+
await driver.close();
14+
exit(result == 'pass' ? 0 : 1);
15+
}

0 commit comments

Comments
 (0)