Skip to content

Commit 1783ea4

Browse files
robert-ancellEgor
authored andcommitted
[shared_preferences_linux] Add support for Linux (flutter#2836)
Adds shared_preferences support for Linux. Part of flutter/flutter#41720
1 parent 4a6bc68 commit 1783ea4

14 files changed

Lines changed: 431 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.DS_Store
2+
.dart_tool/
3+
4+
.packages
5+
.pub/
6+
7+
build/
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: e491544588e8d34fdf31d5f840b4649850ef167a
8+
channel: master
9+
10+
project_type: plugin
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
## 0.0.1
2+
* Initial release to support shared_preferences on Linux.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2020 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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# shared_preferences_linux
2+
3+
The Linux implementation of [`shared_preferences`][1].
4+
5+
## Usage
6+
7+
### Import the package
8+
9+
This package is an unendorsed Linux implementation of `shared_preferences`.
10+
11+
In order to use this now, you'll need to depend on `shared_preferences_linux`.
12+
When this package is endorsed it will be automatically used by the `shared_preferences` package and you can switch to that API.
13+
14+
```yaml
15+
...
16+
dependencies:
17+
...
18+
shared_preferences_linux: ^0.0.1
19+
...
20+
```
21+
22+
[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: e491544588e8d34fdf31d5f840b4649850ef167a
8+
channel: master
9+
10+
project_type: app
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# shared_preferences_example
2+
3+
Demonstrates how to use the shared_preferences plugin.
4+
5+
## Getting Started
6+
7+
For help getting started with Flutter, view our online
8+
[documentation](http://flutter.io/).
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_linux/shared_preferences_linux.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 = SharedPreferencesLinux.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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: shared_preferences_linux_example
2+
description: Demonstrates how to use the shared_preferences_linux plugin.
3+
4+
dependencies:
5+
flutter:
6+
sdk: flutter
7+
shared_preferences_linux: ^0.1.0
8+
9+
dependency_overrides:
10+
shared_preferences_linux:
11+
path: ../
12+
13+
flutter:
14+
uses-material-design: true
15+

0 commit comments

Comments
 (0)