Flutter应用配置Firebase Debug App Check遇阻求助:无法获取Token且调用云函数报错
Let’s tackle your Firebase App Check problems step by step—we’ll resolve the MainActivity crash, get the debug token working, and address those confusing logs.
1. Why Your MainActivity Crashed
The crash happened because you were initializing Firebase twice (once in MainActivity.kt and once in main.dart) and the order was wrong. FlutterFire's Firebase.initializeApp() already handles native Firebase initialization under the hood, so manually calling FirebaseApp.initializeApp(this) in onCreate caused a conflict. Plus, you tried accessing FirebaseAppCheck before letting Flutter set up the Firebase app.
2. Fixed MainActivity.kt Code
Replace your MainActivity.kt with this—we’ll let Flutter handle Firebase initialization first, then attach the debug provider:
import android.os.Bundle import com.google.firebase.appcheck.FirebaseAppCheck import com.google.firebase.appcheck.debug.DebugAppCheckProviderFactory import io.flutter.embedding.android.FlutterActivity class MainActivity : FlutterActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Flutter has already initialized FirebaseApp at this point val firebaseAppCheck = FirebaseAppCheck.getInstance() firebaseAppCheck.installAppCheckProviderFactory( DebugAppCheckProviderFactory.getInstance() ) } }
3. Updated main.dart for Debug Mode
Tell Flutter to use the Android debug provider explicitly—this ensures consistency between native and Flutter layers:
Future<void> main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp( options: DefaultFirebaseOptions.currentPlatform, ); await FirebaseAppCheck.instance.activate( androidProvider: AndroidProvider.debug, // Critical for debug token generation webRecaptchaSiteKey: 'recaptcha-v3-site-key', // Keep this for web only ); await FirebaseAppCheck.instance.setTokenAutoRefreshEnabled(true); runApp(const MyApp()); }
4. Retrieve and Register the Debug Secret
After building and running your app on a physical device/emulator:
- Open Android Studio's Logcat
- Filter logs for
DebugAppCheckProvider - You’ll see a log like:
D DebugAppCheckProvider: Enter this debug secret into the allow list in the Firebase Console for your project: YOUR_DEBUG_SECRET_HERE
- Copy that secret and add it to your Firebase Console:
- Go to App Check > Debug tokens
- Click Add debug token and paste the secret
5. Addressing the Log Warnings
- The
Accessing hidden methodwarnings are internal Firebase reflection calls—they don’t break functionality and can be ignored. If you want to suppress them, enable core library desugaring in yourapp/build.gradle:android { compileOptions { coreLibraryDesugaringEnabled true } } dependencies { coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.4' } - The
ProviderInstallererror is due to outdated/missing Google Play Services on your device. For debug mode, this won’t affect the debug App Check provider, but for release mode with SafetyNet, ensure your device has the latest Google Play Services.
Once you’ve completed these steps, your app should successfully fetch a debug App Check token, and your cloud function calls should work without the FirebaseException: 7: error.
内容的提问来源于stack exchange,提问作者isaac.harrisholt

