Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions packages/shared_preferences/shared_preferences/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.5.2

* Fixed a bug in the example app.
Comment thread
kenzieschmoll marked this conversation as resolved.
Outdated

## 2.5.1

* Exposes `SharedPreferencesOptions`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class SharedPreferencesDemoState extends State<SharedPreferencesDemo> {
late Future<int> _counter;
int _externalCounter = 0;

/// Completes when the preferences have been initialized, which happens after
/// legacy preferences have been migrated.
final Completer<void> _preferencesReady = Completer<void>();

Future<void> _incrementCounter() async {
final SharedPreferencesWithCache prefs = await _prefs;
final int counter = (prefs.getInt('counter') ?? 0) + 1;
Expand All @@ -59,8 +63,9 @@ class SharedPreferencesDemoState extends State<SharedPreferencesDemo> {
/// or via some native system.
Future<void> _getExternalCounter() async {
final SharedPreferencesAsync prefs = SharedPreferencesAsync();
setState(() async {
_externalCounter = (await prefs.getInt('externalCounter')) ?? 0;
final int externalCounter = (await prefs.getInt('externalCounter')) ?? 0;
setState(() {
_externalCounter = externalCounter;
});
}

Expand All @@ -85,6 +90,7 @@ class SharedPreferencesDemoState extends State<SharedPreferencesDemo> {
return prefs.getInt('counter') ?? 0;
});
_getExternalCounter();
_preferencesReady.complete();
});
}

Expand All @@ -95,25 +101,30 @@ class SharedPreferencesDemoState extends State<SharedPreferencesDemo> {
title: const Text('SharedPreferencesWithCache Demo'),
),
body: Center(
child: FutureBuilder<int>(
future: _counter,
builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.waiting:
return const CircularProgressIndicator();
case ConnectionState.active:
case ConnectionState.done:
if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
} else {
return Text(
'Button tapped ${snapshot.data ?? 0 + _externalCounter} time${(snapshot.data ?? 0 + _externalCounter) == 1 ? '' : 's'}.\n\n'
'This should persist across restarts.',
);
}
}
})),
child: _WaitForInitialization(
initialized: _preferencesReady.future,
builder: (BuildContext context) => FutureBuilder<int>(
future: _counter,
builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.waiting:
return const CircularProgressIndicator();
case ConnectionState.active:
case ConnectionState.done:
if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
} else {
return Text(
'Button tapped ${snapshot.data ?? 0 + _externalCounter} time${(snapshot.data ?? 0 + _externalCounter) == 1 ? '' : 's'}.\n\n'
'This should persist across restarts.',
);
}
}
},
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
Expand All @@ -122,3 +133,28 @@ class SharedPreferencesDemoState extends State<SharedPreferencesDemo> {
);
}
}

/// Waits for the [initialized] future to complete before rendering [builder].
class _WaitForInitialization extends StatelessWidget {
const _WaitForInitialization({
required this.initialized,
required this.builder,
});

final Future<void> initialized;
final WidgetBuilder builder;

@override
Widget build(BuildContext context) {
return FutureBuilder<void>(
future: initialized,
builder: (BuildContext context, AsyncSnapshot<void> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting ||
snapshot.connectionState == ConnectionState.none) {
return const CircularProgressIndicator();
}
return builder(context);
},
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences_example/main.dart';
import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart';

import '../..//test/shared_preferences_async_test.dart';
Comment thread
kenzieschmoll marked this conversation as resolved.
Outdated

void main() {
group('SharedPreferences example app', () {
setUp(() {
SharedPreferencesAsyncPlatform.instance = FakeSharedPreferencesAsync();
});

tearDown(() {
SharedPreferencesAsyncPlatform.instance = null;
});

testWidgets('builds successfully', (WidgetTester tester) async {
await tester.pumpWidget(const MyApp());
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Flutter plugin for reading and writing simple key-value pairs.
Wraps NSUserDefaults on iOS and SharedPreferences on Android.
repository: https://github.com/flutter/packages/tree/main/packages/shared_preferences/shared_preferences
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+shared_preferences%22
version: 2.5.1
version: 2.5.2

environment:
sdk: ^3.5.0
Expand Down