Home > Back-end >  Null check operator used on a null value - twitter auth
Null check operator used on a null value - twitter auth

Time:02-18

I tried to implement twitter authentication and did it 1. after a tutorial and 2 after the documentation... both the tutorial and the documentation used a bang-operator (!) in the code and in both cases the bang-operator led to the error "Null check operator used on a null value" - see image below

Error message

I made sure to add the scheme to the androidmanifest.xml file and add the callback URL in the twitter app settings - also I made sure the API keys are matching and I wasn't able to find a tutorial to get around the bang operator... I tried to use if clauses to ensure null safety but I still had to use the bang operator, which in the end, didn't change anything. The code to my function is below

void login() async {
    final twitterLogin = TwitterLogin(
        apiKey: 't4jz7QiK0tkRQNybk2MKlOa3I',
        apiSecretKey: 'KCwmlETZeLyKWmGlyZdQUXArPfdDj8uEIJqrdCJxZ7XRdpT8Lu',
        redirectURI: 'flutter-twitter-login://');

    await twitterLogin.login().then((value) async {
      final twitterAuthCredentials = TwitterAuthProvider.credential(
          accessToken: value.authToken!, secret: value.authTokenSecret!);
      await FirebaseAuth.instance.signInWithCredential(twitterAuthCredentials);
    });
  }

thanks for your help in advance:)

CodePudding user response:

Can you try like this;

void login() async {
    final twitterLogin = TwitterLogin(
        apiKey: 't4jz7QiK0tkRQNybk2MKlOa3I',
        apiSecretKey: 'KCwmlETZeLyKWmGlyZdQUXArPfdDj8uEIJqrdCJxZ7XRdpT8Lu',
        redirectURI: 'flutter-twitter-login://');

    await twitterLogin.login().then((value) async {
final authToken = value.authToken;
final authTokenSecret = value.authTokenSecret;
if (authToken != null && authTokenSecret != null){
final twitterAuthCredentials = TwitterAuthProvider.credential(accessToken: authToken, secret: authTokenSecret);
await FirebaseAuth.instance.signInWithCredential(twitterAuthCredentials);
}
    });
  }
  • Related