-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[shared_preferences] Fix a late initialized error with the example app #8540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e7ad824
[shared_preferences] Fix a late initialized error with the example app
kenzieschmoll a88794e
changelog and version
kenzieschmoll e7267ad
add test
kenzieschmoll 68d0dc7
review comments
kenzieschmoll 5002272
Merge branch 'main' into fix-example
kenzieschmoll eebbef6
version and changelog
kenzieschmoll 5c6209a
fix test
kenzieschmoll File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
packages/shared_preferences/shared_preferences/example/test/example_app_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// 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/services.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:shared_preferences_example/main.dart'; | ||
import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart'; | ||
import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart'; | ||
import 'package:shared_preferences_platform_interface/types.dart'; | ||
|
||
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()); | ||
}); | ||
}); | ||
} | ||
|
||
// Note: this code is duplicated in | ||
// shared_preferences/test/shared_preferences_async_test.dart. Since we cannot | ||
// import the relative path ../../test/shared_preferences_async_test.dart on the | ||
// web platform, we had to copy it here for use in this test library. | ||
base class FakeSharedPreferencesAsync extends SharedPreferencesAsyncPlatform { | ||
final InMemorySharedPreferencesAsync backend = | ||
InMemorySharedPreferencesAsync.empty(); | ||
final List<MethodCall> log = <MethodCall>[]; | ||
|
||
@override | ||
Future<bool> clear( | ||
ClearPreferencesParameters parameters, SharedPreferencesOptions options) { | ||
log.add(MethodCall('clear', <Object>[...?parameters.filter.allowList])); | ||
return backend.clear(parameters, options); | ||
} | ||
|
||
@override | ||
Future<bool?> getBool(String key, SharedPreferencesOptions options) { | ||
log.add(MethodCall('getBool', <String>[key])); | ||
return backend.getBool(key, options); | ||
} | ||
|
||
@override | ||
Future<double?> getDouble(String key, SharedPreferencesOptions options) { | ||
log.add(MethodCall('getDouble', <String>[key])); | ||
return backend.getDouble(key, options); | ||
} | ||
|
||
@override | ||
Future<int?> getInt(String key, SharedPreferencesOptions options) { | ||
log.add(MethodCall('getInt', <String>[key])); | ||
return backend.getInt(key, options); | ||
} | ||
|
||
@override | ||
Future<Set<String>> getKeys( | ||
GetPreferencesParameters parameters, SharedPreferencesOptions options) { | ||
log.add(MethodCall('getKeys', <String>[...?parameters.filter.allowList])); | ||
return backend.getKeys(parameters, options); | ||
} | ||
|
||
@override | ||
Future<Map<String, Object>> getPreferences( | ||
GetPreferencesParameters parameters, SharedPreferencesOptions options) { | ||
log.add(MethodCall( | ||
'getPreferences', <Object>[...?parameters.filter.allowList])); | ||
return backend.getPreferences(parameters, options); | ||
} | ||
|
||
@override | ||
Future<String?> getString(String key, SharedPreferencesOptions options) { | ||
log.add(MethodCall('getString', <String>[key])); | ||
return backend.getString(key, options); | ||
} | ||
|
||
@override | ||
Future<List<String>?> getStringList( | ||
String key, SharedPreferencesOptions options) { | ||
log.add(MethodCall('getStringList', <String>[key])); | ||
return backend.getStringList(key, options); | ||
} | ||
|
||
@override | ||
Future<bool> setBool( | ||
String key, bool value, SharedPreferencesOptions options) { | ||
log.add(MethodCall('setBool', <Object>[key, value])); | ||
return backend.setBool(key, value, options); | ||
} | ||
|
||
@override | ||
Future<bool> setDouble( | ||
String key, double value, SharedPreferencesOptions options) { | ||
log.add(MethodCall('setDouble', <Object>[key, value])); | ||
return backend.setDouble(key, value, options); | ||
} | ||
|
||
@override | ||
Future<bool> setInt(String key, int value, SharedPreferencesOptions options) { | ||
log.add(MethodCall('setInt', <Object>[key, value])); | ||
return backend.setInt(key, value, options); | ||
} | ||
|
||
@override | ||
Future<bool> setString( | ||
String key, String value, SharedPreferencesOptions options) { | ||
log.add(MethodCall('setString', <Object>[key, value])); | ||
return backend.setString(key, value, options); | ||
} | ||
|
||
@override | ||
Future<bool> setStringList( | ||
String key, List<String> value, SharedPreferencesOptions options) { | ||
log.add(MethodCall('setStringList', <Object>[key, value])); | ||
return backend.setStringList(key, value, options); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to put this code somewhere that can be imported in both locations that isn't exported to users?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Short of creating a shared package for testing utilities (which seems overkill just to share code with the example app test), I'm not sure how else to share this code.
I'm not entirely sure why the relative path can be imported successfully from a test for native platforms but not for the web platform. CC @jakemac53 do you know if this behavior is expected from the test package for this case?
Another option is to add
TestOn('vm')
here so that this test does not run for the web platform.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is expected yes, the way apps are hosted on the web we have no way to access URIs outside of the package.
The "root" of the server is either the test directory or the package directory (can't remember which), so you can only access things from the current package and then package: URIs (via synthetic /packages/ paths).
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These sorts of relative paths also really shouldn't be used anyways, they can easily be broken (that file could have dependencies you don't have for instance, and pub won't be able to resolve that for you).
The solution is a shared testing package (no need to publish it).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tarrinneal are you okay landing this with a todo to create a shared testing package or do we need to do that as part of this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might as well :)