6

我有一个 Flutter 插件在我新创建的 Flutter-plugin 项目上成功运行。iOS 和 Android 都能够成功运行这个 Flutter 插件的 example/lib/main.dart 示例代码。

但是,一旦我尝试在另一个 Flutter 项目中使用该插件,就会崩溃。

到目前为止,我已经尝试了两种方法来将我自己编写的插件集成到另一个 Flutter 项目中:

在我的 Flutter 项目中(我尝试使用里面的插件),我转到pubspec.yaml文件并写:

我尝试了...本地路径集成:

dependencies:
  flutter:
    sdk: flutter
  my_plugin_name:
    path: ../../../Flutter_plugins/my_plugin_name/

或者我尝试了...远程集成:

dependencies:
  flutter:
    sdk: flutter
  my_plugin_name:
    git:
      url: https://github.com/XXXX/my_plugin_name.git

两种集成都可以正常工作,并且 VSCode 似乎可以正确导入插件!

但这就是事情崩溃的地方!只要我添加任何东西dependencies,那就是事后崩溃的时候。

为了测试它是否有效,我打开我的 Flutter 项目并转到 lib/main.dart:

在我想要将插件集成到的项目的 lib/main.dart 中,我编写的内容与插件项目的 plugin-example 文件夹中的内容几乎相同(请参阅下面的代码)。

或者换句话说,我尝试运行与 plugin-example 文件夹完全相同的代码,但这次是在我的另一个 Flutter 项目中导入了插件。

因此,我将示例代码粘贴到我的 Flutter 项目的 lib/main.dart 文件中。

在 main.dart 中,我添加了以下导入:

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:my_plugin_name/my_plugin_name.dart';

. 并将代码粘贴到 main.dart 中:

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      platformVersion = await MyPluginNameClass.getVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }
    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Text('$_platformVersion\n'),
        ),
      ),
    );
  }
}

编译器此时没有给我任何错误或警告。一切似乎都很好。

但是,如果我构建并运行,我会得到世界、IOS 和安卓的错误例外 - 为什么??????

在 iOS 上,错误消息是:

2018-12-23 14:32:52.179 xcodebuild[56661:1181076] [MT] PluginLoading: Required plug-in compatibility UUID D76765677-CB11-4D25-A34B-E33DB5A7C231 for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/KZLinkedConsole.xcplugin' not present in DVTPlugInCompatibilityUUIDs
2018-12-23 14:32:52.179 xcodebuild[56661:1181076] [MT] PluginLoading: Required plug-in compatibility UUID D76765677-CB11-4D25-A34B-E33DB5A7C231 for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/ColorSenseRainbow.xcplugin' not present in DVTPlugInCompatibilityUUIDs
2018-12-23 14:32:52.179 xcodebuild[56661:1181076] [MT] PluginLoading: Required plug-in compatibility UUID D76765677-CB11-4D25-A34B-E33DB5A7C231 for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/BBUDebuggerTuckAway.xcplugin' not present in DVTPlugInCompatibilityUUIDs
2018-12-23 14:32:52.180 xcodebuild[56661:1181076] [MT] PluginLoading: Required plug-in compatibility UUID D76765677-CB11-4D25-A34B-E33DB5A7C231 for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/Alcatraz.xcplugin' not present in DVTPlugInCompatibilityUUIDs
** BUILD FAILED **
Xcode's output:
↳
=== BUILD TARGET sqflite OF PROJECT Pods WITH CONFIGURATION Debug ===
/Users/user/Documents/flutter/.pub-cache/git/my_plugin_name-5cc22b5c6d2345ba1ab23a44324b222c68d24ab4/ios/Classes/MyPluginName.m:2:9: fatal error: 'my_plugin_name/my_plugin_name-Swift.h' file not found
#import < my_plugin_name/my_plugin_name-Swift.h>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
Could not build the application for the simulator.
Error launching application on iPhone XS.
Exited (sigterm)

(......我已经尝试过flutter cleanpod update在其他stackoverflow条目中提到过 - 但错误没有改变......)

在 Android 上,错误消息是:

Launching lib/main.dart on Android SDK built for x86 in debug mode...
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:packageDebug'.
> Execution of compression failed.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 25s
Gradle task assembleDebug failed with exit code 1
Exited (sigterm)

有人知道在这里做什么吗?

集成本地或远程 Flutter-plugin 时我错过了什么?

4

1 回答 1

4

原来不是插件添加是问题!

我在 Flutter 应用程序中拥有的大量资产会导致异常。

从我将插件添加到我的应用程序的那一刻起,我的 Flutter 应用程序就崩溃了,这或多或少是一个巧合。这是误导。(因此,我假设异常也可能发生过——稍后当我构建我的 Flutter 应用程序并添加更多代码时。这可能是某种内存问题......或者你告诉我!)

一旦我删除了我的大型资产(665 MB),插件集成就会顺利运行,没有任何异常。

问题转移到另一个StackOverflow问题:如何在我的 Flutter 应用程序中集成大型资产

并且可以关闭此查询。

于 2018-12-25T11:58:17.873 回答