我有一个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-project的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和Android的错误例外情况-为什么?????
在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 clean
或pod 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插件时我会错过什么?
答案 0 :(得分:0)
结果表明不是插件添加问题!
在Flutter应用程序中拥有的大量资产是导致异常的原因。
从我将插件添加到我的应用程序那一刻起,我的Flutter应用程序崩溃肯定是一个偶然的巧合。这是误导。 (因此,我认为异常也可能发生过-稍后,当我构建Flutter应用并添加更多代码时,可能是某种内存问题...或者你告诉我!)
一旦我移除了很大的资产(665 MB),插件集成就会顺利进行,并且没有任何异常。
该问题的难度转为另一个stackoverflow问题:How to integrate a large asset in my Flutter app
此查询可以关闭。