添加 Multidex 支持后,应用程序卡在启动屏幕上

问题描述 投票:0回答:1

添加依赖项(

cloud_firestore
)后,我收到此错误:

App requires Multidex support
    Multidex support is required for your android app to build since the number of methods has exceeded 64k. See https://docs.flutter.dev/deployment/android#enabling-multidex-support for more information. You may pass the --no-multidex flag to skip Flutter's multidex support to use a manual solution.

    Flutter tool can add multidex support. The following file will be added by flutter:

        android/app/src/main/java/io/flutter/app/FlutterMultiDexApplication.java

所以我继续将

multiDexEnabled
设置为
true
。每当我运行应用程序时,它都会卡在启动屏幕上。

flutter run
输出:

Launching lib\main.dart on sdk gphone64 x86 64 in debug mode...
Building with Flutter multidex support enabled.
You are applying Flutter's app_plugin_loader Gradle plugin imperatively using the apply script method, which is deprecated and will be removed in a future release. Migrate to applying Gradle plugins with the declarative plugins block: https://flutter.dev/go/flutter-gradle-plugin-apply

Running Gradle task 'assembleDebug'...                              9.3s
√  Built build\app\outputs\flutter-apk\app-debug.apk.
Installing build\app\outputs\flutter-apk\app-debug.apk...          992ms
E/flutter ( 8467): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: [core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
E/flutter ( 8467): #0      MethodChannelFirebase.app (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:193:5)
E/flutter ( 8467): #1      Firebase.app (package:firebase_core/src/firebase.dart:56:41)
E/flutter ( 8467): #2      FirebaseFirestore.instance (package:cloud_firestore/src/firestore.dart:31:21)
E/flutter ( 8467): #3      firestoreInstance (package:meals/main.dart:9:45)
E/flutter ( 8467): #4      firestoreInstance (package:meals/main.dart)
E/flutter ( 8467): #5      main (package:meals/main.dart:24:9)
E/flutter ( 8467): #6      _runMain.<anonymous closure> (dart:ui/hooks.dart:301:23)
E/flutter ( 8467): #7      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
E/flutter ( 8467): #8      _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
E/flutter ( 8467):
E/flutter ( 8467): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Binding has not yet been initialized.
E/flutter ( 8467): The "instance" getter on the ServicesBinding binding mixin is only available once that binding has been initialized.
E/flutter ( 8467): Typically, this is done by calling "WidgetsFlutterBinding.ensureInitialized()" or "runApp()" (the latter calls the former). Typically this call is done in the "void main()" method. The "ensureInitialized" method is idempotent; calling it multiple times is not harmful. After calling that method, the "instance" getter will return the binding.
E/flutter ( 8467): In a test, one can call "TestWidgetsFlutterBinding.ensureInitialized()" as the first line in the test's "main()" method to initialize the binding.
E/flutter ( 8467): If ServicesBinding is a custom binding mixin, there must also be a custom binding class, like WidgetsFlutterBinding, but that mixes in the selected binding, and that is the class that must be constructed before using the "instance" getter.
E/flutter ( 8467): #0      BindingBase.checkInstance.<anonymous closure> (package:flutter/src/foundation/binding.dart:300:9)
E/flutter ( 8467): #1      BindingBase.checkInstance (package:flutter/src/foundation/binding.dart:381:6)
E/flutter ( 8467): #2      ServicesBinding.instance (package:flutter/src/services/binding.dart:60:54)
E/flutter ( 8467): #3      _findBinaryMessenger (package:flutter/src/services/platform_channel.dart:158:25)
E/flutter ( 8467): #4      BasicMessageChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:204:56)
E/flutter ( 8467): #5      BasicMessageChannel.send (package:flutter/src/services/platform_channel.dart:218:38)
E/flutter ( 8467): #6      FirebaseCoreHostApi.initializeCore (package:firebase_core_platform_interface/src/pigeon/messages.pigeon.dart:208:52)
E/flutter ( 8467): #7      MethodChannelFirebase._initializeCore (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:29:54)
E/flutter ( 8467): #8      MethodChannelFirebase.initializeApp (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:73:13)
E/flutter ( 8467): #9      Firebase.initializeApp (package:firebase_core/src/firebase.dart:43:47)
E/flutter ( 8467): #10     main (package:meals/main.dart:21:12)
E/flutter ( 8467): #11     _runMain.<anonymous closure> (dart:ui/hooks.dart:301:23)
E/flutter ( 8467): #12     _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
E/flutter ( 8467): #13     _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
E/flutter ( 8467):
Syncing files to device sdk gphone64 x86 64...                      99ms

Flutter run key commands.
r Hot reload.
R Hot restart.
h List all available interactive commands.
d Detach (terminate "flutter run" but leave application running).
c Clear the screen
q Quit (terminate the application on the device).

A Dart VM Service on sdk gphone64 x86 64 is available at: http://127.0.0.1:56676/egviUq76mA8=/
D/ProfileInstaller( 8467): Installing profile for com.example.meals

main.dart

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:meals/screens/tabs.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';

final firestoreInstance = FirebaseFirestore.instance;


final theme = ThemeData(
  useMaterial3: true,
  colorScheme: ColorScheme.fromSeed(
      brightness: Brightness.dark,
      seedColor: const Color.fromARGB(255, 3, 107, 225)),
  textTheme: GoogleFonts.latoTextTheme(),
);

void main() {
  Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  runApp(const ProviderScope(
    child: App(),
  ));
}

class App extends StatelessWidget {
  const App({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: theme,
      home: const TabsScreen(),
    );
  }
}

dependencies

  cupertino_icons: ^1.0.2
  google_fonts: ^6.1.0
  transparent_image: ^2.0.1
  flutter_riverpod: ^2.4.6
  image_picker: ^1.0.4
  path_provider: ^2.1.2
  font_awesome_flutter: ^10.7.0
  duration_picker: ^1.1.1
  firebase_core: ^2.27.1
  cloud_firestore: ^4.15.9
android flutter dart android-multidex
1个回答
0
投票

在 main 中尝试这个

WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp().whenComplete(() {
   print('Firebase initialized');
});
© www.soinside.com 2019 - 2024. All rights reserved.