Skip to content

Commit 9737d53

Browse files
committed
完善 lib plugin
1 parent d2dba70 commit 9737d53

File tree

5 files changed

+46
-11
lines changed

5 files changed

+46
-11
lines changed

android/src/main/java/com/sensorsdata/analytics/sensorsanalyticsflutterplugin/SensorsAnalyticsFlutterPlugin.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
import com.sensorsdata.analytics.android.sdk.SALog;
88
import com.sensorsdata.analytics.android.sdk.SensorsDataAPI;
99

10+
import org.json.JSONArray;
1011
import org.json.JSONObject;
1112

13+
import java.util.ArrayList;
1214
import java.util.Collection;
1315
import java.util.HashMap;
1416
import java.util.HashSet;
@@ -214,6 +216,15 @@ private void getSuperProperties(Result result) {
214216
while (keys.hasNext()) {
215217
String key = keys.next();
216218
Object value = jsonObject.opt(key);
219+
if (value instanceof JSONArray) {
220+
JSONArray jsonArray = (JSONArray) value;
221+
if (jsonArray.length() != 0) {
222+
value = new ArrayList();
223+
for (int index = 0; index < jsonArray.length(); index++) {
224+
((ArrayList) value).add(jsonArray.opt(index));
225+
}
226+
}
227+
}
217228
map.put(key, value);
218229
}
219230
result.success(map);

example/lib/main.dart

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class MyApp extends StatefulWidget {
1212
}
1313

1414
class _MyAppState extends State<MyApp> {
15-
String _distinctId = '';
15+
String? _distinctId = '';
1616
var parameters;
1717

1818
@override
@@ -44,7 +44,7 @@ class _MyAppState extends State<MyApp> {
4444

4545
@override
4646
Widget build(BuildContext context) {
47-
dynamic tmpResult;
47+
late dynamic tmpResult;
4848

4949
return MaterialApp(
5050
home: Scaffold(
@@ -85,13 +85,30 @@ class _MyAppState extends State<MyApp> {
8585
title: Text('追踪事件 track'),
8686
onTap: () {
8787
print("======触发事件233");
88+
89+
dynamic a = "aaa";
90+
print(a is String);
91+
String? b= "bbb";
92+
dynamic c = b;
93+
print(c.runtimeType);
94+
print(c is String);
95+
print(c is! String);
96+
print(c is String?);
97+
print("======");
98+
dynamic d = null;
99+
print(d.runtimeType);
100+
print(d is String);
101+
print(d is! String);
102+
print(d is String?);
103+
88104
// SensorsAnalyticsFlutterPlugin.track(
89105
// 'ViewProduct', <String, dynamic>{
90106
// "a_time": DateTime.now(),
91107
// "product_name": "Apple 12 max pro"
92108
// });
93-
var map = {/*r"$lib_plugin_version":["flutter:234"],*/"address":"000000000000"};
94-
SensorsAnalyticsFlutterPlugin.track("hello", map);
109+
List<String?>? list = ["flutter_plugin:12312"];
110+
var map = {r"$lib_plugin_version":list, "address":"000000000000"};
111+
SensorsAnalyticsFlutterPlugin.track("hello", map);
95112
},
96113
),
97114
ListTile(
@@ -286,14 +303,17 @@ class _MyAppState extends State<MyApp> {
286303
ListTile(
287304
title: Text('setsuperproperties'),
288305
onTap: () async {
306+
List<String?>? list = ["flutter_plugin:super_set"];
307+
var map = {r"$lib_plugin_version":list};
289308
SensorsAnalyticsFlutterPlugin.registerSuperProperties(
290-
{"age": 100});
291-
print("getSuperProperties===");
309+
map);
310+
print("setSuperProperties===");
292311
},
293312
),
294313
ListTile(
295314
title: Text('getSuperProperties'),
296315
onTap: () async {
316+
var a = 10;
297317
dynamic map =
298318
await SensorsAnalyticsFlutterPlugin.getSuperProperties();
299319
print("getSuperProperties===$map");

example/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ description: Demonstrates how to use the sensorsanalyticsflutterplugin plugin.
66
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
77

88
environment:
9-
sdk: ">=2.7.0 <3.0.0"
9+
sdk: '>=2.12.0 <3.0.0'
1010

1111
dependencies:
1212
flutter:

example/test/widget_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void main() {
1919
expect(
2020
find.byWidgetPredicate(
2121
(Widget widget) => widget is Text &&
22-
widget.data.startsWith('Running on:'),
22+
widget.data!.startsWith('Running on:'),
2323
),
2424
findsOneWidget,
2525
);

lib/sensors_analytics_flutter_plugin.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class SensorsAnalyticsFlutterPlugin {
2626
/// SensorsAnalyticsFlutterPlugin.track('eventname',{'key1':'value1','key2':'value2'});
2727
///
2828
static void track(String eventName, Map<String, dynamic>? properties) {
29-
properties = properties == null ? null : {...properties};
29+
properties = properties == null ? {} : {...properties};
3030
_convertDateTime(properties);
3131
_setupLibPluginVersion(properties);
3232
List<dynamic> params = [eventName, properties];
@@ -149,7 +149,7 @@ class SensorsAnalyticsFlutterPlugin {
149149
/// SensorsAnalyticsFlutterPlugin.trackViewScreen('urlForView',{'key1':'value1','key2':'value2'});
150150
///
151151
static void trackViewScreen(String url, Map<String, dynamic>? properties) {
152-
properties = properties == null ? null : {...properties};
152+
properties = properties == null ? {} : {...properties};
153153
_convertDateTime(properties);
154154
_setupLibPluginVersion(properties);
155155
List<dynamic> params = [url, properties];
@@ -531,7 +531,11 @@ class SensorsAnalyticsFlutterPlugin {
531531
if (properties == null) {
532532
properties = {};
533533
}
534-
List<String>? values = properties[r"$lib_plugin_version"];
534+
dynamic tmp = properties[r"$lib_plugin_version"];
535+
if ((tmp is! List<String>) && (tmp is! List<String?>)) {
536+
properties.remove(r"$lib_plugin_version");
537+
}
538+
dynamic values = properties[r"$lib_plugin_version"];
535539
values = values == null ? [] : [...values];
536540
values.add("flutter_plugin:$FLUTTER_PLUGIN_VERSION");
537541
properties[r"$lib_plugin_version"] = values;

0 commit comments

Comments
 (0)