dart_objc 0.1.8

  • Readme
  • Changelog
  • Example
  • Installing
  • 46

dart_objc #

Write Objective-C Code using Dart. This package liberates you from native code and low performance channel.

Still under development!!!

pub package

Requirements #

Flutter 1.12.13 (Dart 2.7.0)

Getting Started #

Dart code:

// new Objective-C object.
RuntimeStub stub = RuntimeStub();

// Define Dart function according to Objective-C BarBlock signature.
Function testFunc = (NSObject a) {
    print('hello block! ${a.toString()}');
    return 101;
};

// Dart function will be converted to Objective-C block.
Block block = stub.fooBlock(testFunc);
// invoke Objective-C block.
int result = block.invoke([stub]);
print(result); 

// support built-in structs.
CGRect rect = stub.fooCGRect(CGRect(4, 3, 2, 1));
print(rect);

Objective-C code:

@interface RuntimeStub ()
@end
@implementation RuntimeStub
- (CGRect)fooCGRect:(CGRect)rect
{
    NSLog(@"%s %f, %f, %f, %f", __func__, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
    return (CGRect){1, 2, 3, 4};
}

typedef int(^BarBlock)(NSObject *a);

- (BarBlock)fooBlock:(BarBlock)block
{
    dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^{
        int result = block([NSObject new]);
        NSLog(@"---result: %d", result);
    });
    BarBlock bar = ^(NSObject *a) {
        NSLog(@"bar block arg: %@", a);
        return 404;
    };
    return bar;
}
@end

TODO List #

  • [ ] Generate Dart code from ObjectiveC/C++ Headers.
  • [ ] Unit test.
  • [ ] Documents.

Article #

0.1.8 #

  • Dart function can be observer for NSNotification.
  • Fix issue for automatic transfer of NSRange.

0.1.7 #

  • Update dependancy.

0.1.6 #

  • Update readme.
  • Delete useless code.

0.1.5 #

  • Delete code for banckmark.

0.1.4 #

  • Performance optimization.
  • Bug fix.

0.1.3 #

  • Support more structs: CGPoint, CGVector, CGSize, CGRect, CGAffineTransform, UIEdgeInsets, NSDirectionalEdgeInsets, UIOffset.
  • Support collections: NSArray, NSDictionary, NSSet.
  • Support box/unbox: NSValue, NSNumber.

0.1.2 #

  • Perform method on main/global queue.

0.1.1 #

  • Fix issue for block and delegate callback when returning an object.

0.1.0 #

  • Fix CString and Struct memory issue.

0.1.0-dev.1 #

  • Update SDK constraint to 2.6.0-dev.8.2

0.0.6 #

  • Convert bool Automatically.

0.0.5 #

  • Fix memory leak.

0.0.4 #

  • Support NSString.
  • Fix some memory issue.

0.0.3 #

  • Support delegate callback.

0.0.2 #

  • Format code.

0.0.1 #

  • Initial release.

example/lib/main.dart

import 'package:dart_objc_example/delegatestub.dart';
import 'package:dart_objc_example/runtimestub.dart';
import 'package:flutter/material.dart';
import 'package:dart_objc/dart_objc.dart';

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

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

class _MyAppState extends State<MyApp> {
  RuntimeStub stub = RuntimeStub().retain();
  DelegateStub delegate = DelegateStub().retain();

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

    // bool resultBool = stub.fooBool(false);
    // print('fooBool result:$resultBool');

    // NSString resultNSString = stub.fooNSString('This is NSString');
    // print('fooNSString result:$resultNSString');

    String resultChar = stub.fooChar('A');
    print('fooChar result:$resultChar');

    String resultUChar = stub.fooUChar('A');
    print('fooUChar result:$resultUChar');

    String resultCharPtr = stub.fooCharPtr('test CString');
    print('fooCharPtr result:$resultCharPtr');

    NSObject resultObj = stub.fooObject(delegate);
    print('fooObject result:$resultObj');

    stub.fooDelegate(delegate);

    Block block = stub.fooBlock(testFunc);
    NSObject result = block.invoke([stub]);
    print(result);

    CGSize size = stub.fooCGSize(CGSize(2, 1));
    print(size);

    CGPoint point = stub.fooCGPoint(CGPoint(2, 1));
    print(point);

    CGVector vector = stub.fooCGVector(CGVector(2, 1));
    print(vector);

    CGRect rect = stub.fooCGRect(CGRect(4, 3, 2, 1));
    print(rect);

    NSRange range = stub.fooNSRange(NSRange(2, 1));
    print(range);

    UIEdgeInsets insets = stub.fooUIEdgeInsets(UIEdgeInsets(4, 3, 2, 1));
    print(insets);

    NSDirectionalEdgeInsets dInsets =
        stub.fooNSDirectionalEdgeInsets(NSDirectionalEdgeInsets(4, 3, 2, 1));
    print(dInsets);

    CGAffineTransform transform =
        stub.fooCGAffineTransform(CGAffineTransform(6, 5, 4, 3, 2, 1));
    print(transform);

    NSArray array = stub.fooNSArray([1, 2.345, 'I\'m String', rect]);
    print(array);

    NSObject currentThread = Class('NSThread')
        .perform(Selector('currentThread'), onQueue: DispatchQueue.global());
    NSObject description = currentThread.perform(Selector('description'));
    String threadResult = NSString.fromPointer(description.pointer).value;
    print('currentThread: $threadResult');

    NSObject center =
        Class('NSNotificationCenter').perform(Selector('defaultCenter'));
    // TODO: Only need function.
    center.perform(Selector('addObserver:selector:name:object:'), args: [
      delegate,
      Selector('handleNotification:'),
      'SampleDartNotification',
      nil
    ]);

    // DispatchQueue.main.async(() {
    //   NSObject currentThread = Class('NSThread').perform(Selector('currentThread'));
    //   NSObject description = currentThread.perform(Selector('description'));
    //   String result = NSString.fromPointer(description.pointer).value;
    //   print('currentThread: $result');
    // });
  }

  Function testFunc = (NSObject a) {
    print('hello block! ${a.toString()}');
    return a;
  };

  Future<void> press() async {}

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: MaterialButton(
              child: Text('dialog'),
              onPressed: () {
                press();
              }),
        ),
      ),
    );
  }

  @override
  void dispose() {
    stub.release();
    delegate.release();
    super.dispose();
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  dart_objc: ^0.1.8

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:dart_objc/dart_objc.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
86
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
46
Learn more about scoring.

We analyzed this package on Apr 4, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Health suggestions

Fix lib/src/runtime/functions.dart. (-5.36 points)

Analysis of lib/src/runtime/functions.dart reported 11 hints, including:

line 6 col 45: Name non-constant identifiers using lowerCamelCase.

line 11 col 45: Name non-constant identifiers using lowerCamelCase.

line 16 col 45: Name non-constant identifiers using lowerCamelCase.

line 21 col 35: Name non-constant identifiers using lowerCamelCase.

line 25 col 45: Name non-constant identifiers using lowerCamelCase.

Fix lib/src/foundation/nsvalue.dart. (-3.45 points)

Analysis of lib/src/foundation/nsvalue.dart reported 7 hints, including:

line 72 col 15: Name non-constant identifiers using lowerCamelCase.

line 78 col 16: Name non-constant identifiers using lowerCamelCase.

line 84 col 14: Name non-constant identifiers using lowerCamelCase.

line 90 col 14: Name non-constant identifiers using lowerCamelCase.

line 96 col 25: Name non-constant identifiers using lowerCamelCase.

Fix lib/src/common/precompile_macro.dart. (-1.99 points)

Analysis of lib/src/common/precompile_macro.dart reported 4 hints:

line 5 col 22: Name non-constant identifiers using lowerCamelCase.

line 8 col 22: Name non-constant identifiers using lowerCamelCase.

line 12 col 6: Name non-constant identifiers using lowerCamelCase.

line 13 col 6: Name non-constant identifiers using lowerCamelCase.

Fix additional 5 files with analysis or formatting issues. (-3.99 points)

Additional issues in the following files:

  • lib/src/foundation/gcd.dart (4 hints)
  • lib/src/foundation/basic_type.dart (1 hint)
  • lib/src/foundation/nsnumber.dart (1 hint)
  • lib/src/runtime/id.dart (1 hint)
  • lib/src/runtime/nsobject_protocol.dart (1 hint)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
ffi ^0.1.3 0.1.3
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test