'App Check from Firebase in Flutter doesn't work

I'm trying to use App Check from Firebase with Flutter (on Android only for now), but the documentation is just terrible and I don't understand the errors I get when I try to access a Firebase service with an App Check token.

I run my app on my personal Android device through the terminal using the following command: flutter run --no-sound-null-safety.

I want to access data on Firebase Storage with that app. I have enforced App Check on that feature, I have also enabled SafetyNet for my app and I have created a debug token entitled "tests" that I don't even know how to use.

Here is the error I get:

My error

Here is my main() function:

import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_app_check/firebase_app_check.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  await FirebaseAppCheck.instance.activate();

  // ... not related code

  runApp(MyWidget())
}

Here is my build.gradle in /android/app/:

// ...

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.google.firebase:firebase-appcheck-safetynet:16.0.0-beta03'
}

Here is my MainActivity.kt in /android/app/src/main/kotlin/com/example/my_app/:

// the package

import io.flutter.embedding.android.FlutterActivity
import android.os.Bundle
import com.google.firebase.FirebaseApp
import com.google.firebase.appcheck.FirebaseAppCheck
import com.google.firebase.appcheck.safetynet.SafetyNetAppCheckProviderFactory

class MainActivity: FlutterActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        FirebaseApp.initializeApp(/*context=*/ this);
        val firebaseAppCheck = FirebaseAppCheck.getInstance()
        firebaseAppCheck.installAppCheckProviderFactory(SafetyNetAppCheckProviderFactory.getInstance())
        super.onCreate(savedInstanceState)
    }
}

Please help me guys, nothing works now.



Solution 1:[1]

FirebaseApp.initializeApp(/*context=*/ this);

You have a semicolon, It's Kotlin code; in Kotlin we don't use semicolons.

Solution 2:[2]

in my main.dart

  await FirebaseAppCheck.instance.activate();  

Here is my build.gradle

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.google.firebase:firebase-appcheck-debug:16.0.0-beta06'
}

MainActivity.kt

 import io.flutter.embedding.android.FlutterActivity
    import android.os.Bundle
    import com.google.firebase.FirebaseApp
    import com.google.firebase.appcheck.FirebaseAppCheck;
    import com.google.firebase.appcheck.debug.DebugAppCheckProviderFactory
    
    
class MainActivity: FlutterActivity() {
 override fun onCreate(savedInstanceState: Bundle?) {
        FirebaseApp.initializeApp(/*context=*/this)
        val firebaseAppCheck = FirebaseAppCheck.getInstance()
        firebaseAppCheck.installAppCheckProviderFactory(
            DebugAppCheckProviderFactory.getInstance()
        )
        super.onCreate(savedInstanceState)
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jeremy Caney
Solution 2 Gbenga B Ayannuga