Making Flutter C/C++ interop example work

1,939

You have to import /path/to/project/native_add/example/lib/native_add.dart, because you have imported native_add plugin instead.

Share:
1,939
kakyo
Author by

kakyo

Updated on December 14, 2022

Comments

  • kakyo
    kakyo over 1 year

    I'm learning Flutter-C/C++ interop according to the official example. However, the code I wrote according to the tutorial doesn't compile. There must be some misunderstanding but I have no idea where. Step 1 and 2 seem easy enough and I don't have errors, but after Step 3, the generated example code /path/to/project/native_add/example/lib/main.dart looks like this

    import 'package:flutter/material.dart';
    import 'dart:async';
    
    import 'package:flutter/services.dart';
    import 'package:native_add/native_add.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 NativeAdd.platformVersion;
        } 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('1 + 2 == ${nativeAdd(1, 2)}'),
            ),
          ),
        );
      }
    }
    

    /path/to/project/native_add/example/lib/native_add.dart looks like this

    import 'dart:ffi';  // For FFI
    import 'dart:io';   // For Platform.isX
    
    // Define physical lib: the lib binary file to integrate
    final DynamicLibrary nativeAddLib =
    Platform.isAndroid
        ? DynamicLibrary.open("libnative_add.so")
        : DynamicLibrary.open("native_add.framework/native_add");
    
    // Retrieve exportable API in the lib binary
    final int Function(int x, int y) nativeAdd =
    nativeAddLib
        .lookup<NativeFunction<Int32 Function(Int32, Int32)>>("native_add")
        .asFunction();
    
    

    Running main.dart gives me errors:

    Launching lib/main.dart on iPhone 11 Pro Max in debug mode...
    
    Compiler message:
    lib/main.dart:52:35: Error: Method not found: 'nativeAdd'.
              child: Text('1 + 2 == ${nativeAdd(1, 2)}'),
                                      ^^^^^^^^^
    lib/main.dart:52:35: Error: The method 'nativeAdd' isn't defined for the class '_MyAppState'.
     - '_MyAppState' is from 'package:native_add_example/main.dart' ('lib/main.dart').
    Try correcting the name to the name of an existing method, or defining a method named 'nativeAdd'.
              child: Text('1 + 2 == ${nativeAdd(1, 2)}'),
                                      ^^^^^^^^^
    Compiler failed on /Users/me/Desktop/_dev/playground/flutter/flutter_firstflutterapp_part2/native_add/example/lib/main.dart
    Error launching application on iPhone 11 Pro Max.
    
    

    Where am I wrong?

    • TejaDroid
      TejaDroid about 4 years
      Hi, Can you please provide github repository of native_add, I have same requirement so I need to learn C/C++ support in flutter