Search code examples
firebaseflutterdarttwitterfirebase-authentication

Null check operator used on a null value - twitter auth


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:)


that's the output I get when I run it with your changes:

I/OpenGLRenderer(30256): Davey! duration=41126ms; Flags=1, FrameTimelineVsyncId=13810903, IntendedVsync=184078927234535, Vsync=184078927234535, InputEventId=0, HandleInputStart=184078927898552, AnimationStart=184078927900531, PerformTraversalsStart=184078927901313, DrawStart=184078928491104, FrameDeadline=184078969956757, FrameInterval=184078927890948, FrameStartTime=11111111, SyncQueued=184078929216469, SyncStart=184078929288605, IssueDrawCommandsStart=184078929574907, SwapBuffers=184078941790637, FrameCompleted=184120054049576, DequeueBufferDuration=3825678, QueueBufferDuration=558334, GpuCompleted=184120054049576, SwapBuffersCompleted=184078942707877, DisplayPresentTime=-6758907574315243998,

Edit 2 Im now getting an HttpException: Failed Forbidden error... Ive had it before but I sadly wasn't able to find a solution to it since I don't have a clue where it comes from - see screenshot below

HttpException: Failed Forbidden


Solution

  • 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);
    }
        });
      }