【问题标题】:flutter android main function is called twiceflutter android main函数被调用了两次
【发布时间】:2022-11-28 11:18:49
【问题描述】:

在我的 flutter 应用程序中,我注意到 main 函数执行了两次。我正在使用来自计数器应用程序的示例代码。文本 main function 被打印了两次。我的 AndroidManifest.xml 在下面

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" package="com.app.chat">

    <application android:label="rosta" android:icon="@mipmap/ic_launcher">


        <service android:name="io.wazo.callkeep.VoiceConnectionService" android:exported="true" android:label="Wazo" android:permission="android.permission.BIND_TELECOM_CONNECTION_SERVICE" tools:ignore="IntentFilterExportedReceiver">
            <intent-filter android:exported="true">
                <action android:name="android.telecom.ConnectionService" />
            </intent-filter>
        </service>
        <activity android:name=".MainActivity" android:exported="true" android:launchMode="singleTop" android:theme="@style/LaunchTheme" android:showWhenLocked="true" android:turnScreenOn="true" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize">
         
            <meta-data android:exported="true" android:name="io.flutter.embedding.android.NormalTheme" android:resource="@style/NormalTheme" />
           
            <meta-data android:exported="true" android:name="io.flutter.embedding.android.SplashScreenDrawable" android:resource="@drawable/launch_background" />
            <intent-filter android:exported="true">
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <meta-data android:exported="true" android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="high_importance_channel" />

        
        <meta-data android:name="flutterEmbedding" android:value="2" />
    </application>
</manifest>

以下是计数器应用程序代码。

import 'package:flutter/material.dart';

void main() {
  print('main functions');
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        //

        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    //

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          //

          //

          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

【问题讨论】:

  • 你解决了吗?

标签: android flutter


【解决方案1】:

我曾经在一个 flutter 项目中遇到过同样的问题,并注意到这是由于具有后台服务的依赖项导致了 flutter main 函数的第二次调用。也许您有这样的依赖项(例如清单中的 ConnectionService),它正在调用 main 函数。

您可以尝试删除此类可能的依赖项候选者并再次检查行为。

【讨论】:

  • 您如何找到导致 main() 被调用两次的依赖项?我面临同样的问题,但在 iOS 中而不是在我当前项目的 android 中,它包含太多依赖项。
【解决方案2】:

如果您最近遇到这个问题,您的 main() 函数被调用两次并且您使用 firebase_messaging 版本 14.1.0,您应该将您的包版本升级到 14.1.2

https://github.com/firebase/flutterfire/issues/9912

【讨论】:

    猜你喜欢
    • 2022-11-28
    • 2019-11-01
    • 2012-04-23
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 2020-05-03
    相关资源
    最近更新 更多