Skip to content

Commit e5bd9e9

Browse files
committed
Add workaround for the AndroidMetadata checks
The metaDataAsMap method doesn't work during tests and throws a null check error. A better solution should be implemented when possible.
1 parent a77a0cc commit e5bd9e9

File tree

5 files changed

+16
-13
lines changed

5 files changed

+16
-13
lines changed

analysis_options.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ linter:
6565
empty_statements: true
6666
exhaustive_cases: true
6767
file_names: true
68-
flutter_style_todos: true
68+
flutter_style_todos: false
6969
hash_and_equals: true
7070
implementation_imports: true
7171
iterable_contains_unrelated_type: true

lib/providers/auth.dart

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class AuthProvider with ChangeNotifier {
3939
String? serverUrl;
4040
String? serverVersion;
4141
PackageInfo? applicationVersion;
42-
Map<String, String>? metadata = {};
42+
Map<String, String> metadata = {};
4343

4444
static const MIN_APP_VERSION_URL = 'min-app-version';
4545
static const SERVER_VERSION_URL = 'version';
@@ -48,13 +48,16 @@ class AuthProvider with ChangeNotifier {
4848

4949
late http.Client client;
5050

51-
AuthProvider([http.Client? client]) {
51+
AuthProvider([http.Client? client, bool? checkMetadata]) {
5252
this.client = client ?? http.Client();
5353

54-
try {
55-
AndroidMetadata.metaDataAsMap.then((value) => metadata = value);
56-
} on PlatformException {
57-
throw Exception('An error occurred reading the metadata from AndroidManifest');
54+
// TODO: this is a workaround since AndroidMetadata doesn't work while running tests
55+
if (checkMetadata ?? true) {
56+
try {
57+
AndroidMetadata.metaDataAsMap.then((value) => metadata = value!);
58+
} on PlatformException {
59+
throw Exception('An error occurred reading the metadata from AndroidManifest');
60+
} catch (error) {}
5861
}
5962
}
6063

@@ -80,8 +83,8 @@ class AuthProvider with ChangeNotifier {
8083

8184
/// Checking if there is a new version of the application.
8285
Future<bool> applicationUpdateRequired([String? version]) async {
83-
if (metadata!.containsKey('wger.check_min_app_version') ||
84-
metadata!['wger.check_min_app_version'] == 'false') {
86+
if (metadata.containsKey('wger.check_min_app_version') ||
87+
metadata['wger.check_min_app_version'] == 'false') {
8588
return false;
8689
}
8790

@@ -110,7 +113,7 @@ class AuthProvider with ChangeNotifier {
110113
makeUri(serverUrl, REGISTRATION_URL),
111114
headers: {
112115
HttpHeaders.contentTypeHeader: 'application/json; charset=UTF-8',
113-
HttpHeaders.authorizationHeader: 'Token ${metadata![MANIFEST_KEY_API]}',
116+
HttpHeaders.authorizationHeader: 'Token ${metadata[MANIFEST_KEY_API]}',
114117
HttpHeaders.userAgentHeader: getAppNameHeader(),
115118
},
116119
body: json.encode(data),

lib/screens/auth_screen.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class _AuthCardState extends State<AuthCard> {
119119
// If not, the user will not be able to register via the app
120120
try {
121121
final metadata = Provider.of<AuthProvider>(context, listen: false).metadata;
122-
if (metadata!.containsKey(MANIFEST_KEY_API) || metadata[MANIFEST_KEY_API] == '') {
122+
if (metadata.containsKey(MANIFEST_KEY_API) || metadata[MANIFEST_KEY_API] == '') {
123123
_canRegister = false;
124124
}
125125
} on PlatformException {

test/auth/auth_provider_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void main() {
1717

1818
setUp(() {
1919
mockClient = MockClient();
20-
authProvider = AuthProvider(mockClient);
20+
authProvider = AuthProvider(mockClient, false);
2121
authProvider.serverUrl = 'http://localhost';
2222
});
2323

test/utils.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import '../test_data/exercises.dart';
2323
import 'other/base_provider_test.mocks.dart';
2424

2525
// Test Auth provider
26-
final AuthProvider testAuthProvider = AuthProvider(MockClient())
26+
final AuthProvider testAuthProvider = AuthProvider(MockClient(), false)
2727
..token = 'FooBar'
2828
..serverUrl = 'https://localhost';
2929

0 commit comments

Comments
 (0)