Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Flutter plugin that works in another Flutter project?

I have a Flutter plugin successfully running on my newly created Flutter-plugin project. And both worlds, iOS and Android, are able to run the example/lib/main.dart example-code of this Flutter-plugin successfully.

However, once I try to use the plugin in another Flutter project, that's when things crash.

So far, I have tried two ways of how to integrate my self-written plugin into another Flutter project:

Inside my Flutter project (the one I try to use the plugin inside), I go to the pubspec.yaml file and I write:

I tried ...local path integration:

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

or I tried ...remote integration:

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

Both integrations work fine and VSCode seem to import the plugin correctly !

BUT THIS IS WHERE THINGS CRASH!! AS SOON AS I ADD ANYTHING TO THE dependencies, THAT IS WHEN THINGS CRASH AFTERWARDS.

To test if it works, I open my Flutter project and I go to lib/main.dart:

And inside lib/main.dart of the project I want to integrate the plugin into, I write pretty much the same as was written in the plugin-example folder of the plugin-project (see code further down).

Or in other words, I try to run exactly the same code that was given by the plugin-example folder, but this time inside my other Flutter project having the plugin imported.

I therefore paste the example-code into my lib/main.dart file of my Flutter project.

Inside main.dart, I add the following imports:

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

. And the code pasted into 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'),
        ),
      ),
    );
  }
}

The compiler does not give me any error or warning at this point. All seems fine.

HOWEVER, IF I BUILD AND RUN, I GET ERROR EXCEPTIONS FOR BOTH WORLDS, IOS AND ANDROID - WHY ?????

On iOS, the error messages are:

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)

(...I already tried flutter clean or pod update as was mentioned in other stackoverflow entries - but no change on the error...)

On Android, the error messages are:

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)

Does anybody have any clue on what to do here ?

What do I miss when integrating a local or remote Flutter-plugin ?

like image 348
iKK Avatar asked Dec 23 '18 14:12

iKK


People also ask

What is the difference between package and plugin?

plugin= some functionality of tool. Package= something which allows us to develop new thing.


1 Answers

Turns out that not the plugin-adding is the problem!

It is rather my large asset that I have inside the Flutter app that causes the exception.

And it must be more or less a coincidence that my Flutter app crashes from the moment I add the plugin to my app. It was misleading. (I therefore assume that the exception might have happened as well - later when I build-up my Flutter app and add more code. It might be a memory issue of some sort...or you tell me!)

As soon as I remove my large asset (665 MB), the plugin integration works smoothly without any exception.

The question shifts tough to another StackOverflow question: How to integrate a large asset in my Flutter app

And this query can be closed.

like image 120
iKK Avatar answered Sep 18 '22 14:09

iKK