Skip to content
This repository was archived by the owner on Jul 8, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class _MyAppState extends State<MyApp> {

switch (result.status) {
case FacebookLoginStatus.loggedIn:
final FacebookAccessToken accessToken = result.accessToken;
final FacebookAccessToken accessToken = result.accessToken!;
_showMessage('''
Logged in!

Expand Down
28 changes: 11 additions & 17 deletions lib/flutter_facebook_login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class FacebookLogin {
///
/// Ignored on iOS, as it's not supported by the iOS Facebook Login SDK anymore.
set loginBehavior(FacebookLoginBehavior behavior) {
assert(behavior != null, 'The login behavior cannot be null.');
_loginBehavior = behavior;
}

Expand Down Expand Up @@ -82,8 +81,8 @@ class FacebookLogin {
///
/// NOTE: This might return an access token that has expired. If you need to be
/// sure that the token is still valid, call [isValid] on the access token.
Future<FacebookAccessToken> get currentAccessToken async {
final Map<dynamic, dynamic> accessToken =
Future<FacebookAccessToken?> get currentAccessToken async {
final Map<dynamic, dynamic>? accessToken =
await channel.invokeMethod('getCurrentAccessToken');

if (accessToken == null) {
Expand All @@ -104,14 +103,13 @@ class FacebookLogin {
Future<FacebookLoginResult> logIn(
List<String> permissions,
) async {
final Map<dynamic, dynamic> result =
await channel.invokeMethod('logIn', {
final result = await channel.invokeMethod<Map<dynamic, dynamic>>('logIn', {
'behavior': _currentLoginBehaviorAsString(),
'permissions': permissions,
});

return _deliverResult(
FacebookLoginResult._(result.cast<String, dynamic>()));
FacebookLoginResult._(result?.cast<String, dynamic>() ?? {}));
}

/// Logs the currently logged in user out.
Expand All @@ -127,8 +125,6 @@ class FacebookLogin {
Future<void> logOut() async => channel.invokeMethod('logOut');

String _currentLoginBehaviorAsString() {
assert(_loginBehavior != null, 'The login behavior was unexpectedly null.');

switch (_loginBehavior) {
case FacebookLoginBehavior.nativeWithFallback:
return 'nativeWithFallback';
Expand All @@ -139,8 +135,6 @@ class FacebookLogin {
case FacebookLoginBehavior.webViewOnly:
return 'webViewOnly';
}

throw StateError('Invalid login behavior.');
}

/// There's a weird bug where calling Navigator.push (or any similar method)
Expand Down Expand Up @@ -212,13 +206,13 @@ class FacebookLoginResult {
///
/// Only available when the [status] equals [FacebookLoginStatus.loggedIn],
/// otherwise null.
final FacebookAccessToken accessToken;
final FacebookAccessToken? accessToken;

/// The error message when the log in flow completed with an error.
///
/// Only available when the [status] equals [FacebookLoginStatus.error],
/// otherwise null.
final String errorMessage;
final String? errorMessage;

FacebookLoginResult._(Map<String, dynamic> map)
: status = _parseStatus(map['status']),
Expand All @@ -229,7 +223,7 @@ class FacebookLoginResult {
: null,
errorMessage = map['errorMessage'];

static FacebookLoginStatus _parseStatus(String status) {
static FacebookLoginStatus _parseStatus(String? status) {
switch (status) {
case 'loggedIn':
return FacebookLoginStatus.loggedIn;
Expand Down Expand Up @@ -264,10 +258,10 @@ enum FacebookLoginStatus {
class FacebookAccessToken {
/// The access token returned by the Facebook login, which can be used to
/// access Facebook APIs.
final String token;
final String? token;

/// The id for the user that is associated with this access token.
final String userId;
final String? userId;

/// The date when this access token expires.
final DateTime expires;
Expand All @@ -277,14 +271,14 @@ class FacebookAccessToken {
/// These are the permissions that were requested with last login, and which
/// the user approved. If permissions have changed since the last login, this
/// list might be outdated.
final List<String> permissions;
final List<String>? permissions;

/// The list of declined permissions associated with this access token.
///
/// These are the permissions that were requested, but the user didn't
/// approve. Similarly to [permissions], this list might be outdated if these
/// permissions have changed since the last login.
final List<String> declinedPermissions;
final List<String>? declinedPermissions;

/// Is this access token expired or not?
///
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_facebook_login
description: A Flutter plugin for allowing users to authenticate with native Android &amp; iOS Facebook login SDKs.
version: 3.0.0
version: 3.0.0-nullsafety
author: Iiro Krankka <iiro.krankka@gmail.com>
homepage: https://github.com/roughike/flutter_facebook_login

Expand All @@ -19,4 +19,4 @@ flutter:
pluginClass: FacebookLoginPlugin

environment:
sdk: ">=2.0.0-dev.28.0 <3.0.0"
sdk: '>=2.12.0-133.7.beta <3.0.0'
23 changes: 9 additions & 14 deletions test/facebook_login_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import 'package:flutter_test/flutter_test.dart';
import 'custom_matchers.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();

group('$FacebookLogin', () {
const channel = MethodChannel('com.roughike/flutter_facebook_login');

Expand Down Expand Up @@ -41,9 +43,9 @@ void main() {
};

final log = <MethodCall>[];
FacebookLogin sut;
late FacebookLogin sut;

void setMethodCallResponse(Map<String, dynamic> response) {
void setMethodCallResponse(Map<String, dynamic>? response) {
channel.setMockMethodCallHandler((methodCall) {
log.add(methodCall);
return Future.value(response);
Expand Down Expand Up @@ -94,7 +96,7 @@ void main() {
setMethodCallResponse(kLoggedInResponse);

final result = await sut.logIn([]);
final map = result.accessToken.toMap();
final map = result.accessToken!.toMap();

expect(
map,
Expand Down Expand Up @@ -124,13 +126,6 @@ void main() {
expect(first, equals(second));
});

test('loginBehavior - with null argument', () async {
setMethodCallResponse(null);

// Setting a null login behavior is not allowed.
expect(() => sut.loginBehavior = null, throwsAssertionError);
});

test('loginBehavior - nativeWithFallback is the default', () async {
setMethodCallResponse(kCancelledByUserResponse);

Expand Down Expand Up @@ -187,7 +182,7 @@ void main() {
]);

expect(result.status, FacebookLoginStatus.loggedIn);
expectAccessTokenParsedCorrectly(result.accessToken);
expectAccessTokenParsedCorrectly(result.accessToken!);

expect(
log,
Expand Down Expand Up @@ -279,7 +274,7 @@ void main() {
setMethodCallResponse(kAccessToken);

final accessToken = await sut.currentAccessToken;
expectAccessTokenParsedCorrectly(accessToken);
expectAccessTokenParsedCorrectly(accessToken!);
});

test('FacebookAccessToken#isValid() - when not expired, returns true',
Expand All @@ -289,7 +284,7 @@ void main() {
Clock.dateTimeResolver = () => beforeExpiry;

final accessToken = await sut.currentAccessToken;
expect(accessToken.isValid(), isTrue);
expect(accessToken!.isValid(), isTrue);
});

test('FacebookAccessToken#isValid() - when expired, returns false',
Expand All @@ -299,7 +294,7 @@ void main() {
Clock.dateTimeResolver = () => afterExpiry;

final accessToken = await sut.currentAccessToken;
expect(accessToken.isValid(), isFalse);
expect(accessToken!.isValid(), isFalse);
});
});
}