local_auth iOS (Flutter) - ios

Flutter local_auth didn't return platform exception when biometrics and passcode are disable for iOS.
I have a functionality where if biometrics and passcode are disable, I will use another function for authentication but it only works on Android and didn't work on iOS.
Future<bool> authentication() async {
try {
return await _localAuthentication.authenticate(
localizedReason: Strings.localAuthReason,
useErrorDialogs: true,
stickyAuth: true,
);
} on PlatformException {
return false;
}
}

import this code below
import 'package:local_auth/error_codes.dart' as local_auth_error;
and try this
Future<bool> authentication() async {
try {
return await _localAuthentication.authenticate(
localizedReason: Strings.localAuthReason,
useErrorDialogs: true,
stickyAuth: true,
);
} on PlatformException catch (exception) {
if (exception.code == local_auth_error.notAvailable ||
exception.code == local_auth_error.passcodeNotSet ||
exception.code == local_auth_error.notEnrolled) {
// Handle this exception here.
}
}
}

Related

Flutter - iOS fcm notification click redirect on app killed state not working

Here initial message for fcm
RemoteMessage? initialMessage = await FirebaseMessaging.instance.getInitialMessage();
debugPrint('notificationKey:${initialMessage?.data[keyAction]}');
if (initialMessage != null) {
String remoteActionKey = initialMessage.data[keyAction] ?? blankString;
switch (remoteActionKey) {
case _Action.like:
//todo condition
break;
case _Action.connection:
//todo condition
break;
default:
break;
}
}
Main method :
Future<void> main() async {
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
}
Global method:
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
}
Then we get the data like this:
FirebaseMessaging.instance
.getInitialMessage().then((RemoteMessage? message) async {
if (message != null) {
//route
}
});

Why can't catch exceptions with "catchError" in Dart?

Test:
void testAs() async {
try {
String b = await test();
print(b);
} catch (e) {
print("1 await error");
}
test().then((value) => print(value)).catchError(() {
print("2 then error");
});
}
Future<String> test() {
List<String> bb = ["2222"];
return Future.value(bb[1]);
}
1 await error
RangeError (index): Invalid value: Only valid value is 0: 1
Why is it ineffective?
If I want to deal with "future" through "then", how should I catch the exception and not let it throw out.
Thank you friends,the last problem has been solved,the problem can be solved by adding async and await flags to the test() method.
But there is a new problem, Now I use the correct code and find that it can only be printed once.why can't it print "then success",then the program ends
,modify as follows:
void testAs() async {
try {
await test();
print("await success");
} catch (e) {
print("await error");
}
test().then((value) => print("then success")).catchError((e) {
print("then error");
});
}
Future<String> test() async{
List<String> bb = ["2222"];
return await Future.value(bb[0]);
}
print:await success

Firebase automatic login failes on iOS

I'm developing an application using flutter. I'm facing an error when user logs in the application on an android device, terminates the app and opens the app again, the _firebaseAuth.currentUser returns the current user details, but when the same is done on iOS, it returns null. Can anyone suggest why this happens. The plugin version is firebase_auth 0.18.0+1. Sharing the code for checking the
`
bool isSignedIn() {
try {
final currentUser = _firebaseAuth.currentUser;
return currentUser != null;
} catch (error) {
return false;
}
}
`
i've used bloc like this
`
runApp(BlocProvider(
create: (_) => AuthBloc(userRepository: _userRepository)..add(AppStarted()),
child: MyApp(),
));
`
`
Stream<AuthState> _mapAppStarted() async* {
try {
final isSignedIn = _userRepository.isSignedIn();
if (isSignedIn) {
final user = await _userRepository.getUser();
if (user == null) {
await _userRepository.signOut();
yield Unauthenticated();
} else {
List<FollowedFollowingModel> followedFollowingList =
await getFollowingList(user);
yield Authenticated(user, followedFollowingList);
}
} else {
yield Unauthenticated();
}
} catch (_) {
yield Unauthenticated();
}
}
`
In the main.dart file inside a Material widget you can use it like following:
home: FutureBuilder<FirebaseApp>(
future: Firebase.initializeApp(),
builder: (BuildContext context, AsyncSnapshot<FirebaseApp> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return FirebaseAuth.instance.currentUser == null
? GettingStarted()
: Dashboard();
}
/// other way there is no user logged.
return Scaffold();
}),

How to handle user cancel for OAUTH2 request in Teams messaging extension

The cancellation response from token.botframework.com is currently being displayed to screen like this:
{
"error": {
"code": "ServiceError",
"message": "Missing required query string parameter: code. Url = https://token.botframework.com/.auth/web/redirect?state=d48fb60ae4834fd8adabfe054a5eff74&error_description=The+user+chose+not+to+give+your+app+access+to+their+Dropbox+account.&error=access_denied"
}
}
How can I, instead, handle the cancellation gracefully? If the user cancels like this, I'd like to just have the auth-card-popup window close automatically.
This is for an action-type messaging extension app that I'm building. The sign-in process begins with an auth card. The bot is pointed at a Dropbox OAUTH2 connection. Here the relevant code that brings up the card:
const { TeamsActivityHandler, CardFactory } = require('botbuilder');
class MsgExtActionBot extends TeamsActivityHandler {
constructor() {
super();
this.connectionName = 'oauth2-provider';
}
async handleTeamsMessagingExtensionFetchTask(context, action) {
if (!await this.isAuthenticated(context)) {
return this.getSignInResponse(context);
}
}
async isAuthenticated(context) {
let tokenResponse = await context.adapter.getUserToken(
context,
this.connectionName
);
if (tokenResponse && tokenResponse.token) {
return true;
}
if (!context.activity.value.state) {
return false;
}
tokenResponse = await context.adapter.getUserToken(
context,
this.connectionName,
context.activity.value.state
);
if (tokenResponse && tokenResponse.token) {
return true;
}
return false;
}
async getSignInResponse(context) {
const signInLink = await context.adapter.getSignInLink(context, this.connectionName);
return {
composeExtension: {
type: 'auth',
suggestedActions: {
actions: [{
type: 'openUrl',
value: signInLink,
title: 'Please sign in'
}]
},
}
};
}
}

How to setState() of a text field after handling an error in Flutter?

I have a function called LoginWithFb(). The function has a try catch block:
void loginWithFb() async {
try {
var auth = AuthProvider.of(context).auth;
print('Signing up with fb...');
setState(() {
_showProgressIndicator = true;
});
FirebaseUser user = await auth.signInWithFBAcc();
uId = user?.uid;
if (uId != null) {
print('Signed in: $uId');
widget.onSignedIn(user);
} else {
print('fb login cancelled');
}
// _showAlert(context);
setState(() {
_showProgressIndicator = false;
});
} catch (exception) {
print(exception.toString());
setState(() {
_showProgressIndicator = false;
});
}
When the error is caught, I want to display message to the user. The message has to be in a text field and not via a dialog. At the moment I have an empty Text('') widget in my build method. I want to write text to the text widget when the error is caught..
Just use local variable for storing message and show it via Text widget
String message = "";
void loginWithFb() async {
try {
...
} catch (exception) {
print(exception.toString());
setState(() {
message = "Some error happens";
_showProgressIndicator = false;
});
}
In widget:
Text(message);

Resources