@@ -2,6 +2,7 @@ import 'dart:convert';
22import 'dart:io' show Platform;
33import 'package:flutter/foundation.dart' ;
44import 'package:flutter/services.dart' ;
5+ import 'package:flutter/widgets.dart' ;
56import 'package:plugin_platform_interface/plugin_platform_interface.dart' ;
67
78part 'corner_radius_plugin_platform_interface.dart' ;
@@ -75,6 +76,8 @@ class CornerRadiusPlugin {
7576 ///
7677 /// Returns a [Future<CornerRadius>] representing the initialized plugin instance.
7778 static Future <CornerRadius > init () async {
79+ // Ensure bindings are initialized before any asset/binary messenger usage.
80+ WidgetsFlutterBinding .ensureInitialized ();
7881 if (Platform .isIOS) {
7982 final info = await CornerRadiusPluginPlatform ._instance.getDeviceInfo ();
8083 if (info == null ||
@@ -85,10 +88,19 @@ class CornerRadiusPlugin {
8588 }
8689 final modelId = info["modelIdentifier" ]! ;
8790 final deviceType = info["deviceType" ]! ;
88-
91+ String jsonStr;
92+ try {
93+ jsonStr = await rootBundle.loadString (
94+ 'packages/corner_radius_plugin/assets/bezel.min.json' ,
95+ );
96+ } catch (_) {
97+ _screenRadius = CornerRadius ._default ();
98+ return _screenRadius;
99+ }
89100 final radius = await compute (_lookupBezelForModel, {
90101 'modelId' : modelId,
91102 'deviceType' : deviceType,
103+ 'json' : jsonStr,
92104 });
93105 if (radius == null || radius == 0 ) {
94106 _screenRadius = CornerRadius ._default ();
@@ -113,35 +125,63 @@ class CornerRadiusPlugin {
113125Future <double ?> _lookupBezelForModel (Map <String , dynamic > params) async {
114126 final modelId = params['modelId' ] as String ;
115127 final deviceType = params['deviceType' ] as String ;
128+ // final RootIsolateToken? token = params['token'] as RootIsolateToken?;
129+ final String providedJson = params['json' ] as String ;
116130
117131 try {
118- final bundle = rootBundle;
119- final jsonStr = await bundle.loadString (
120- 'packages/corner_radius_plugin/assets/bezel.min.json' ,
121- );
122- final dynamic decoded = json.decode (jsonStr);
123- if (decoded is Map <String , dynamic >) {
124- // Try type-scoped lookup first
125- final typeMap = decoded[deviceType];
126- if (typeMap is Map <String , dynamic >) {
127- final device = typeMap[modelId];
128- if (device is Map <String , dynamic >) {
129- final val = device['bezel' ];
130- if (val is num ) return val.toDouble ();
131- }
132+ // Prefer JSON provided from the main isolate to avoid asset loading here.
133+ final dynamic decoded = json.decode (providedJson);
134+ if (decoded is ! Map <String , dynamic >) return 0 ;
135+
136+ // Dataset shape:
137+ // { "_metadata": {...}, "devices": { "iPhone": { "iPhone14,2": { "bezel": 47.33 } }, "iPad": {...} } }
138+ final devicesRoot = decoded['devices' ];
139+ if (devicesRoot is ! Map <String , dynamic >) return 0 ;
140+
141+ double ? parseBezel (dynamic deviceEntry) {
142+ if (deviceEntry is ! Map <String , dynamic >) return null ;
143+ final dynamic v =
144+ deviceEntry['bezel' ] ??
145+ deviceEntry['bazel' ] ??
146+ deviceEntry['radius' ] ??
147+ deviceEntry['cornerRadius' ];
148+ if (v is num ) return v.toDouble ();
149+ if (v is String ) {
150+ final d = double .tryParse (v);
151+ if (d != null ) return d;
132152 }
153+ return null ;
154+ }
155+
156+ // 1) Try direct type -> model lookup
157+ final typeMap = devicesRoot[deviceType];
158+ if (typeMap is Map <String , dynamic >) {
159+ final device = typeMap[modelId];
160+ final parsed = parseBezel (device);
161+ if (parsed != null ) return parsed;
162+ }
163+
164+ // 2) Try well-known iOS families if deviceType didn't match nesting
165+ const fallbackFamilies = ['iPhone' , 'iPad' , 'iPod' ];
166+ for (final fam in fallbackFamilies) {
167+ final famMap = devicesRoot[fam];
168+ if (famMap is Map <String , dynamic >) {
169+ final device = famMap[modelId];
170+ final parsed = parseBezel (device);
171+ if (parsed != null ) return parsed;
172+ }
173+ }
133174
134- // Fallback to flat devices map
135- final devices = decoded['devices' ];
136- if (devices is Map <String , dynamic >) {
137- final device = devices[modelId];
138- if (device is Map <String , dynamic >) {
139- final val = device['bezel' ];
140- if (val is num ) return val.toDouble ();
141- }
175+ // 3) Last resort: search all types for the modelId
176+ for (final entry in devicesRoot.values) {
177+ if (entry is Map <String , dynamic >) {
178+ final device = entry[modelId];
179+ final parsed = parseBezel (device);
180+ if (parsed != null ) return parsed;
142181 }
143182 }
144- } catch (_) {
183+ } catch (e, st) {
184+ debugPrint ('Error loading bezel data: $e \n $st ' );
145185 return 0 ;
146186 }
147187 return 0 ;
0 commit comments