1
- # dart_native
1
+ # Dart_Native
2
2
3
- Write native code using Dart. This package liberates you from native code and low performance channel .
3
+ Dart_Native operates as both a code generator tool and a bridge to communicate between Dart and native APIs .
4
4
5
- Still under development!!!
5
+ Replaces the low-performing Flutter channel with faster and more concise code.
6
+
7
+ * Under development
6
8
7
9
[ ![ pub package] ( https://img.shields.io/pub/v/dart_native.svg )] ( https://pub.dev/packages/dart_native )
8
10
[ ![ Build Status] ( https://travis-ci.org/dart-native/dart_native.svg?branch=master )] ( https://travis-ci.org/dart-native/dart_native )
9
11
10
- This is the blue part(DartNative Bridge) in the picture below :
12
+ This package is the blue part(DartNative Bridge):
11
13
12
14
![ ] ( images/dartnative.png )
13
15
14
- ## 📲 Requirement
16
+ ## Requirements
15
17
16
- | DartNative Version | Requirements |
18
+ | Dart_Native Version | Requirements |
17
19
| --- | --- |
18
20
| 0.3.0 | Flutter 1.20.0 (Dart 2.9.1) |
19
21
| 0.2.0 | Flutter 1.12.13 (Dart 2.7) |
20
22
21
- ## 🌟 Feature
23
+ ## Supported Platforms
24
+
25
+ iOS & Android
26
+
27
+ ## Usage
28
+
29
+ 1 . Add ``` dart_native ``` to dependencies and ``` build_runner ``` to dev_dependencies.
22
30
23
- - [x] Support iOS & Android platform.
24
- - [x] Sync/Async channel with high performance.
25
- - [x] Generate Dart bridging code from native code.
26
- - [x] Automatic object marshalling between Dart and native.
31
+ 2 . Generate Dart wrapper code with [ @dartnative/codegen ] ( https://www.npmjs.com/package/@dartnative/codegen ) or write Dart code manually.
27
32
28
- ## 🔮 Getting Started
33
+ 3 . Generate code for automatic type conversion using [ dart_native_gen] ( https://pub.dev/packages/dart_native_gen ) with the following steps (3.1-3.3):
34
+
35
+ 3.1 Annotate a Dart wrapper class with ` @native ` .
36
+ ``` dart
37
+ @native
38
+ class RuntimeSon extends RuntimeStub {
39
+ RuntimeSon([Class isa]) : super(Class('RuntimeSon'));
40
+ RuntimeSon.fromPointer(Pointer<Void> ptr) : super.fromPointer(ptr);
41
+ }
42
+ ```
43
+
44
+ 3.2 Annotate your own entry (such as`main()`) with `@nativeRoot`.
45
+
46
+ ```dart
47
+ @nativeRoot
48
+ void main() {
49
+ runApp(App());
50
+ }
51
+ ```
52
+
53
+ 3.3 Run
54
+ ```bash
55
+ flutter packages pub run build_runner build --delete-conflicting-outputs
56
+ ```
57
+ to generate files into your source directory.
58
+
59
+ Note: we recommend running `clean` first:
60
+
61
+ ```bash
62
+ flutter packages pub run build_runner clean
63
+ ```
64
+
65
+ 4. Call autogenerated function in `<generated-name>.dn.dart` in 3.3. The function name is determined by `name` in `pubspec.yaml`.
66
+
67
+ ```dart
68
+ @nativeRoot
69
+ void main() {
70
+ // Function name is generated by name in pubspec.yaml.
71
+ runDartNativeExample();
72
+ runApp(App());
73
+ }
74
+ ```
29
75
76
+ ## Features
77
+
78
+ ### High-performance synchronous & asynchronous channeling
79
+
80
+ Dart_Native costs significantly less time than the Flutter channel and supports both synchronous and asynchronous channeling. A comparison of two native channeling tasks using Flutter channel and Dart_Native is shown below.
81
+
82
+ Both tasks were executed 10,000 times in the same environment using either Flutter channel or Dart_Native:
83
+
84
+ | Task | Total time cost (ms) (Channel/Dart_Native) | Channeling time cost (ms) (Channel/Dart_Native) |
85
+ | --- | --- | --- |
86
+ | Checking if an app needs to be installed | 5202/4166 | 919/99 |
87
+ | Logging | 2480/2024 | 1075/432 |
88
+
89
+
90
+ ### Autogenerate succinct bridging code
91
+
92
+ Dart_Native supports automatic type conversion so its bridging code is shorter & simpler than the Flutter channel.
93
+
94
+ A comparison of the task of "checking if an app needs to be installed" is shown below:
95
+
96
+ | | # of lines of bridging code | Coding complexity |
97
+ | --- | --- | --- |
98
+ | Dart_Native | Dart 1 + Native 1 | Autogenerated code returns BOOL directly |
99
+ | Channel | Dart 15 + Native 30 | Needs to manually define return format, convert INT to BOOL, determine channel & methodName |
100
+
101
+ ### Automatic object marshalling between Dart and native
102
+
103
+ ## Examples
30
104
##### iOS:
31
105
32
- Dart code:
106
+ Dart code (generated) :
33
107
34
108
```dart
35
109
// new Objective-C object.
@@ -46,7 +120,7 @@ CGRect rect = stub.fooCGRect(CGRect(4, 3, 2, 1));
46
120
print(rect);
47
121
48
122
```
49
- Objective-C code:
123
+ Corresponding Objective-C code:
50
124
51
125
``` objc
52
126
typedef int (^BarBlock)(NSObject * a);
@@ -59,11 +133,11 @@ typedef int(^BarBlock)(NSObject *a);
59
133
@end
60
134
```
61
135
62
- More ios example see: [ios_unit_test.dart](/dart_native/example/lib/ios/unit_test.dart)
136
+ More iOS examples see: [ios_unit_test.dart](/dart_native/example/lib/ios/unit_test.dart)
63
137
64
138
##### Android:
65
139
66
- Dart code:
140
+ Dart code (generated) :
67
141
```dart
68
142
// new Java object.
69
143
RuntimeStub stub = RuntimeStub();
@@ -75,7 +149,7 @@ List list = stub.getList([1, 2, 3, 4]);
75
149
stub.setDelegateListener(DelegateStub());
76
150
77
151
```
78
- Java code:
152
+ Corresponding Java code:
79
153
80
154
``` java
81
155
public class RuntimeStub {
@@ -92,15 +166,15 @@ public class RuntimeStub {
92
166
}
93
167
}
94
168
```
95
- More android example see: [ android_unit_test.dart] ( /dart_native/example/lib/android/unit_test.dart )
96
- ## 📚 Document
169
+ More android examples see: [ android_unit_test.dart] ( /dart_native/example/lib/android/unit_test.dart )
170
+ ## Documentation
97
171
98
172
### Readme
99
173
100
174
1 . [ dart_native README.md] ( /dart_native/README.md )
101
175
2 . [ dart_native_gen README.md] ( /dart_native_gen/README.md )
102
176
103
- ### Blog
177
+ ### Further reading
104
178
105
179
- [ 告别 Flutter Channel,调用 Native API 仅需一行代码!] ( http://yulingtianxia.com/blog/2020/06/25/Codegen-for-DartNative/ )
106
180
- [ 如何实现一行命令自动生成 Flutter 插件] ( http://yulingtianxia.com/blog/2020/07/25/How-to-Implement-Codegen/ )
@@ -112,23 +186,21 @@ More android example see: [android_unit_test.dart](/dart_native/example/lib/andr
112
186
- [ 在 Flutter 中玩转 Objective-C Block] ( http://yulingtianxia.com/blog/2020/03/28/Using-Objective-C-Block-in-Flutter/ )
113
187
- [ Passing Out Parameter in DartNative] ( http://yulingtianxia.com/blog/2020/04/25/Passing-Out-Parameter-in-DartNative/ )
114
188
115
- ## 🐒 Q&A
189
+ ## FAQs
116
190
117
191
Q: Failed to lookup symbol (dlsym(RTLD_DEFAULT, InitDartApiDL): symbol not found) on iOS archive.
118
192
119
- There are two Workarounds:
120
-
121
- 1 . Use dynamic library: Add ` use_frameworks! ` in Podfile.
122
- 2 . Select Target Runner -> Build Settings -> Strip Style -> change from "All
123
- Symbols" to "Non-Global Symbols"
193
+ A: Select one solution:
194
+ 1 . Use dynamic library: Add ` use_frameworks! ` in Podfile.
195
+ 2 . Select Target Runner -> Build Settings -> Strip Style -> change from "All Symbols" to "Non-Global Symbols"
124
196
125
- ## ❤️ Contributed
197
+ ## Contribution
126
198
127
199
- If you ** need help** or you'd like to ** ask a general question** , open an issue.
128
200
- If you ** found a bug** , open an issue.
129
201
- If you ** have a feature request** , open an issue.
130
202
- If you ** want to contribute** , submit a pull request.
131
203
132
- ## 👮🏻 License
204
+ ## License
133
205
134
- DartNative is available under the BSD 3-Clause License. See the LICENSE file for more info.
206
+ DartNative is available under the BSD 3-Clause License. See the LICENSE file for more info.
0 commit comments