Skip to content

Commit 617841b

Browse files
committed
make null safe
1 parent 4fc8b0c commit 617841b

34 files changed

Lines changed: 361 additions & 151 deletions

lib/auth-data.dart

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,29 @@ class AuthData {
1919
this.response,
2020
});
2121

22-
final String userID; // User's profile id
23-
final String clientID; // OAuth client id
24-
final String accessToken; // OAuth access token
25-
final String firstName; // User's first name
26-
final String lastName; // User's last name
27-
final String email; // User's email
28-
final String profileImgUrl; // User's profile image url
29-
final Map<String, dynamic> userJson; // Full returned user json
30-
final Map<String, String> response; // Full returned auth response.
22+
final String? userID; // User's profile id
23+
final String? clientID; // OAuth client id
24+
final String? accessToken; // OAuth access token
25+
final String? firstName; // User's first name
26+
final String? lastName; // User's last name
27+
final String? email; // User's email
28+
final String? profileImgUrl; // User's profile image url
29+
final Map<String, dynamic>? userJson; // Full returned user json
30+
final Map<String, String>? response; // Full returned auth response.
3131

3232
/// Creates a formatted string from
3333
/// the response data.
3434
String _formatResponse() {
3535
StringBuffer result = StringBuffer('\n');
3636

37-
for (MapEntry data in response.entries) {
38-
result.write('\t\t\t\t');
39-
result.write(data.key);
40-
result.write(' = ');
41-
result.write(data.value);
42-
result.write('\n');
37+
if (response != null) {
38+
for (MapEntry data in response!.entries) {
39+
result.write('\t\t\t\t');
40+
result.write(data.key);
41+
result.write(' = ');
42+
result.write(data.value);
43+
result.write('\n');
44+
}
4345
}
4446

4547
return result.toString();

lib/discord.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ class DiscordAuth extends Visa {
1313
final Debug _debug = Debug(prefix: 'In DiscordAuth ->');
1414

1515
@override
16-
SimpleAuth visa;
17-
16+
late SimpleAuth visa;
1817
DiscordAuth() {
1918
visa = SimpleAuth(
2019
baseUrl: baseUrl,
@@ -24,7 +23,7 @@ class DiscordAuth extends Visa {
2423
getAuthData: (Map<String, String> oauthData) async {
2524
if (debugMode) _debug.info('OAuth Data: $oauthData');
2625

27-
final String token = oauthData[OAuth.TOKEN_KEY];
26+
final String? token = oauthData[OAuth.TOKEN_KEY];
2827
if (debugMode) _debug.info('OAuth token: $token');
2928

3029
// User profile API endpoint.
@@ -47,7 +46,7 @@ class DiscordAuth extends Visa {
4746
@override
4847
AuthData authData(
4948
Map<String, dynamic> profileJson, Map<String, String> oauthData) {
50-
final String accessToken = oauthData[OAuth.TOKEN_KEY];
49+
final String? accessToken = oauthData[OAuth.TOKEN_KEY];
5150
final String userId = profileJson['id'] as String;
5251
final String avatar = profileJson['avatar'] as String;
5352
final String profileImgUrl = 'https://cdn.discordapp.com/'

lib/engine/debug.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'package:flutter/material.dart';
22

33
class Debug {
4-
Debug({@required this.prefix});
4+
Debug({required this.prefix});
55

66
String prefix;
77

lib/engine/oauth.dart

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@ import 'package:webview_flutter/webview_flutter.dart';
1010
/// for OAuth 2.0 Authentication.
1111
class OAuth {
1212
OAuth(
13-
{@required this.baseUrl,
14-
@required this.clientID,
15-
@required this.redirectUri,
16-
@required this.state,
17-
@required this.scope,
18-
@required this.debugMode,
13+
{required this.baseUrl,
14+
required this.clientID,
15+
required this.redirectUri,
16+
required this.state,
17+
required this.scope,
18+
required this.debugMode,
1919
this.clientSecret,
2020
this.responseType,
2121
this.otherQueryParams});
2222

2323
final String baseUrl; // OAuth url
2424
final String clientID; // OAuth clientID
25-
final String clientSecret; // OAuth clientSecret
26-
final String responseType; // OAuth clientSecret
25+
final String? clientSecret; // OAuth clientSecret
26+
final String? responseType; // OAuth clientSecret
2727
final String redirectUri; // OAuth redirectUri
2828
final String state; // OAuth state
2929
final String scope; // OAuth scope
30-
final Map<String, String> otherQueryParams;
30+
final Map<String, String>? otherQueryParams;
3131
final bool debugMode; // Debug mode?
3232
static const String TOKEN_KEY = 'access_token'; // OAuth token key
3333
static const String CODE_KEY = 'code'; // OAuth code key
@@ -45,16 +45,16 @@ class OAuth {
4545
/// Sets up a [WebView] for OAuth authentication.
4646
/// [onDone] is called when authentication is
4747
/// completed successfully.
48-
WebView authenticate({@required Function onDone, bool clearCache = false}) {
48+
WebView authenticate({required Function onDone, bool clearCache = false}) {
4949
String clientSecretQuery =
5050
clientSecret != null ? '&client_secret=$clientSecret' : '';
5151
String responseTypeQuery =
5252
'&response_type=${responseType == null ? 'token' : responseType}';
5353
String otherParams = '';
5454

5555
if (otherQueryParams != null) {
56-
for (String key in otherQueryParams.keys) {
57-
otherParams += '&$key=${otherQueryParams[key]}';
56+
for (String key in otherQueryParams!.keys) {
57+
otherParams += '&$key=${otherQueryParams![key]}';
5858
}
5959
}
6060

@@ -103,14 +103,14 @@ class OAuth {
103103
returnedData[STATE_KEY] = state;
104104

105105
if (clientSecret != null) {
106-
returnedData[CLIENT_SECRET_KEY] = clientSecret;
106+
returnedData[CLIENT_SECRET_KEY] = clientSecret!;
107107
}
108108

109109
onDone(returnedData);
110110
} else if (debugMode) {
111111
_debug.info('Redirect Url Not Found');
112112
_debug.info('Url = $url');
113-
_debug.info('Redirect Url = $redirectUri');
113+
//_debug.info('Redirect Url = $redirectUri');
114114
}
115115

116116
return NavigationDecision.navigate;

lib/engine/simple-auth.dart

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ class SimpleAuth {
1515
/// Creates a new instance based on the given OAuth
1616
/// baseUrl and getAuthData function.
1717
SimpleAuth(
18-
{@required this.baseUrl,
19-
@required this.getAuthData,
18+
{required this.baseUrl,
19+
required this.getAuthData,
2020
this.responseType,
2121
this.otherQueryParams});
2222

2323
final String baseUrl; // OAuth base url
24-
final String responseType;
25-
final Map<String, String> otherQueryParams;
24+
final String? responseType;
25+
final Map<String, String>? otherQueryParams;
2626

2727
/// This function makes the necessary api calls to
2828
/// get a user's profile data. It accepts a single
@@ -43,12 +43,12 @@ class SimpleAuth {
4343
/// provided credentials. Returns a WebView
4444
/// That's been set up for authentication
4545
WebView authenticate(
46-
{@required String clientID,
47-
String clientSecret,
48-
@required String redirectUri,
49-
@required String state,
50-
@required String scope,
51-
@required Function onDone,
46+
{required String clientID,
47+
String? clientSecret,
48+
required String redirectUri,
49+
required String state,
50+
required String scope,
51+
required Function onDone,
5252
bool newSession = false}) {
5353
final OAuth oAuth = OAuth(
5454
baseUrl: baseUrl,
@@ -65,9 +65,8 @@ class SimpleAuth {
6565
clearCache: newSession,
6666
onDone: (responseData) async {
6767
if (debugMode) _debug.info('Response: $responseData');
68-
69-
final String token = responseData[OAuth.TOKEN_KEY];
70-
final String code = responseData[OAuth.CODE_KEY];
68+
final String? token = responseData[OAuth.TOKEN_KEY];
69+
final String? code = responseData[OAuth.CODE_KEY];
7170

7271
AuthData authData = token == null && code == null
7372
? AuthData(response: responseData)

lib/engine/visa.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import 'simple-auth.dart';
55
/// Visa abstract class
66
abstract class Visa {
77
/// a [SimpleAuth] instance
8-
SimpleAuth visa;
8+
late SimpleAuth visa;
99

1010
/// Debug mode?
1111
bool debugMode = false;

lib/fb.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class FacebookAuth extends Visa {
1313
final Debug _debug = Debug(prefix: 'In FacebookAuth ->');
1414

1515
@override
16-
SimpleAuth visa;
16+
late SimpleAuth visa;
1717

1818
FacebookAuth() {
1919
visa = SimpleAuth(
@@ -24,7 +24,7 @@ class FacebookAuth extends Visa {
2424
getAuthData: (Map<String, String> oauthData) async {
2525
if (debugMode) _debug.info('OAuth Data: $oauthData');
2626

27-
final String token = oauthData[OAuth.TOKEN_KEY];
27+
final String? token = oauthData[OAuth.TOKEN_KEY];
2828
if (debugMode) _debug.info('OAuth token: $token');
2929

3030
// User profile API endpoint.
@@ -47,7 +47,7 @@ class FacebookAuth extends Visa {
4747
/// to build an [AuthData] object.
4848
@override
4949
AuthData authData(Map<String, dynamic> json, Map<String, String> data) {
50-
final String accessToken = data[OAuth.TOKEN_KEY];
50+
final String? accessToken = data[OAuth.TOKEN_KEY];
5151
final String profileImgUrl = 'https://graph.facebook.com/me/picture'
5252
'?type=large'
5353
'&access_token=$accessToken';

lib/github.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class GithubAuth extends Visa {
1313
final Debug _debug = Debug(prefix: 'In GithubAuth ->');
1414

1515
@override
16-
SimpleAuth visa;
16+
late SimpleAuth visa;
1717

1818
GithubAuth() {
1919
visa = SimpleAuth(
@@ -27,7 +27,7 @@ class GithubAuth extends Visa {
2727
if (debugMode) _debug.info('OAuth Data: $oauthData');
2828

2929
await _getToken(oauthData);
30-
final String token = oauthData[OAuth.TOKEN_KEY];
30+
final String? token = oauthData[OAuth.TOKEN_KEY];
3131
if (debugMode) _debug.info('OAuth token: $token');
3232

3333
// User profile API endpoint.
@@ -51,7 +51,7 @@ class GithubAuth extends Visa {
5151
/// to build an [AuthData] object.
5252
AuthData authData(
5353
Map<String, dynamic> profileJson, Map<String, String> oauthData) {
54-
final String accessToken = oauthData[OAuth.TOKEN_KEY];
54+
final String? accessToken = oauthData[OAuth.TOKEN_KEY];
5555

5656
return AuthData(
5757
clientID: oauthData[OAuth.CLIENT_ID_KEY],
@@ -108,7 +108,7 @@ class GithubAuth extends Visa {
108108

109109
if (debugMode) _debug.info('Returned Profile Json: $profileJson');
110110

111-
if (profileJson['name'] != null){
111+
if (profileJson['name'] != null) {
112112
final List<String> name = profileJson['name'].split(' ');
113113
profileJson['first_name'] = name[0];
114114
profileJson['last_name'] = name[1];
@@ -131,7 +131,7 @@ class GithubAuth extends Visa {
131131
_debug.info(
132132
'In GithubAuth -> Returned Email Response: ${emailResponse.body}');
133133

134-
String email;
134+
String? email;
135135

136136
for (var _email in emailJson) {
137137
if (_email['primary']) {

lib/google.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class GoogleAuth extends Visa {
1414
String personFields;
1515

1616
@override
17-
SimpleAuth visa;
17+
late SimpleAuth visa;
1818

1919
GoogleAuth({this.personFields = ""}) {
2020
personFields = _getPersonFields(personFields);
@@ -27,7 +27,7 @@ class GoogleAuth extends Visa {
2727
getAuthData: (Map<String, String> oauthData) async {
2828
if (debugMode) _debug.info('OAuth Data: $oauthData');
2929

30-
final String token = oauthData[OAuth.TOKEN_KEY];
30+
final String? token = oauthData[OAuth.TOKEN_KEY];
3131
if (debugMode) _debug.info('OAuth token: $token');
3232

3333
// User profile API endpoint.
@@ -51,7 +51,7 @@ class GoogleAuth extends Visa {
5151
/// to build an [AuthData] object.
5252
AuthData authData(
5353
Map<String, dynamic> profileJson, Map<String, String> oauthData) {
54-
final String accessToken = oauthData[OAuth.TOKEN_KEY];
54+
final String? accessToken = oauthData[OAuth.TOKEN_KEY];
5555

5656
return AuthData(
5757
clientID: oauthData['clientID'],

lib/linkedin.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class LinkedInAuth extends Visa {
1313
final Debug _debug = Debug(prefix: 'In LinkedInAuth ->');
1414

1515
@override
16-
SimpleAuth visa;
16+
late SimpleAuth visa;
1717

1818
LinkedInAuth() {
1919
visa = SimpleAuth(
@@ -28,7 +28,7 @@ class LinkedInAuth extends Visa {
2828
if (debugMode) _debug.info('OAuth Data: $oauthData');
2929

3030
await _getToken(oauthData);
31-
final String token = oauthData[OAuth.TOKEN_KEY];
31+
final String? token = oauthData[OAuth.TOKEN_KEY];
3232
if (debugMode) _debug.info('OAuth token: $token');
3333

3434
final String baseApiUrl = 'https://api.linkedin.com/v2';
@@ -52,7 +52,7 @@ class LinkedInAuth extends Visa {
5252
/// to build an [AuthData] object.
5353
AuthData authData(
5454
Map<String, dynamic> profileJson, Map<String, String> oauthData) {
55-
final String accessToken = oauthData[OAuth.TOKEN_KEY];
55+
final String? accessToken = oauthData[OAuth.TOKEN_KEY];
5656

5757
return AuthData(
5858
clientID: oauthData[OAuth.CLIENT_ID_KEY],
@@ -141,7 +141,7 @@ class LinkedInAuth extends Visa {
141141
_debug.info('Returned Email Response: ${emailResponse.body}');
142142
}
143143

144-
String email;
144+
String? email;
145145
List<dynamic> elements = emailJson['elements'];
146146

147147
for (Map<String, dynamic> contact in elements) {

0 commit comments

Comments
 (0)