Error only appears in release mode and not in debug mode

好久不见. 提交于 2020-07-10 10:48:27

问题


Everything is working fine in debug mode but when I run the application in release mode, it produces this screen: 1

and when I try hosting the application on firebase, I get this screen: 2

I am not sure how to handle this error as there is not much info on this issue. But based on the "No firebase app" error, i think that the error might be in my main.dart.

main.dart

void main() {
  assert(() {
    fb.initializeApp(
      apiKey: "",
      authDomain: "",
      databaseURL: "",
      projectId: "",
      storageBucket: "",
      messagingSenderId: "",
    );

    return true;
  }());

  WidgetsFlutterBinding.ensureInitialized();
  BlocSupervisor.delegate = GeneralBlocDelegate();

  runApp(MultiRepositoryProvider(
      providers: [
        RepositoryProvider<UserRepository>(
            builder: (context) => UserRepository()),
      ],
      child: MultiBlocProvider(providers: [
        BlocProvider<AuthBloc>(
          builder: (context) => AuthBloc(
              userRepository: RepositoryProvider.of<UserRepository>(context))
            ..add(AppStarted()),
        ),
      ], child: App())));
}

index.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>my website</title>
</head>
<body>
  <script src="main.dart.js" type="application/javascript"></script>

  <script src="https://www.gstatic.com/firebasejs/7.2.1/firebase-app.js"></script>
  <script src="https://www.gstatic.com/firebasejs/7.2.1/firebase-auth.js"></script>
  <script src="https://www.gstatic.com/firebasejs/7.2.1/firebase-firestore.js"></script>

  <script src="/__/firebase/7.2.2/firebase-app.js"></script>
  <script src="/__/firebase/7.2.2/firebase-auth.js"></script>
  <script src="/__/firebase/7.2.2/firebase-firestore.js"></script>
  <script src="/__/firebase/init.js"></script>

</body>
</html>

pubspec.yaml

description: A new Flutter project.
version: 1.0.0+1

environment:
  sdk: ">=2.1.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^0.1.2

  firebase: ^6.0.0
  flutter_bloc: ^1.0.0
  equatable: ^0.6.1
  font_awesome_flutter: ^8.5.0

dev_dependencies:
  flutter_test:
    sdk: flutter

flutter:
  uses-material-design: true
  assets:
    - assets/images/

回答1:


Problem 1: Using assert() function to initialize app

You are calling the fb.initializeApp() method inside the assert() method which will not be called in the release build. assert method is useful during development to add some additional checks. It should not contain or be used to handle, any functional/business logic. Check this section of the dart language tour. It states that assert method is ignored in production mode.

In production code, assertions are ignored, and the arguments to assert aren’t evaluated.

Problem 2: Missing asset information in pupspec.yaml

In your index.html I see you are trying to load some .js files like this /__/firebase/init.js. Flutter build will not keep this in the build folder unless you mark it as an asset in your pubspec.yaml.

Assuming you placed all your .js files you need for your application in a folder named /js/init.js like the following.

In my case I have js/ folder parallel to the lib/. This is my choice but you can choose which way you want it.

You need an entry like this in your pubspec.yaml.

assets:
  - js/init.js
  - js/firebase-app.js

OR Just include the entire folder like this.

assets:
  - js/

The same applies for all the assets like images, data json etc... Check this reference for more details.

Possible Problem 3: Incorrectly referring local .js file in Index.html

In your index.html you are trying to load some local .js file as mentioned earlier. I think you should load these files as shown below if you follow my suggested folder structure. You should probably edit it based on your own choice.


<script src="assets/js/init.js" type="application/javascript"></script>

<script src="assets/js/firebase-app.js" type="application/javascript"></script>

Hope his helps. Happy Fluttering.



来源:https://stackoverflow.com/questions/58592783/error-only-appears-in-release-mode-and-not-in-debug-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!