Skip to content

Commit 8b17f0a

Browse files
committed
Fixes and changes to DynamicView placeholder replacement logic
- Added support for array references in double curlies - Fix for placeholders not working for nested DynamicView
1 parent 89a2b73 commit 8b17f0a

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

packages/stac/lib/src/parsers/widgets/stac_dynamic_view/stac_dynamic_view_parser.dart

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,53 @@ class StacDynamicViewParser extends StacParser<StacDynamicView> {
9595

9696
dynamic _extractNestedData(dynamic data, List<String> keys) {
9797
dynamic current = data;
98+
final RegExp arrayKeyRegex = RegExp(r'(\w+)\[(\d+)\]');
99+
98100
for (final key in keys) {
99-
if (current is Map && current.containsKey(key)) {
100-
current = current[key];
101+
Match? arrayMatch = arrayKeyRegex.firstMatch(key);
102+
103+
if (arrayMatch != null) {
104+
final String actualKey = arrayMatch.group(1)!;
105+
final int index = int.parse(arrayMatch.group(2)!);
106+
107+
if (current is Map && current.containsKey(actualKey)) {
108+
dynamic potentialList = current[actualKey];
109+
if (potentialList is List) {
110+
if (index >= 0 && index < potentialList.length) {
111+
current = potentialList[index];
112+
} else {
113+
return null;
114+
}
115+
} else {
116+
return null;
117+
}
118+
} else {
119+
return null;
120+
}
101121
} else {
102-
return null;
122+
if (current is Map && current.containsKey(key)) {
123+
current = current[key];
124+
} else if (current is List) {
125+
try {
126+
int index = int.parse(key);
127+
if (index >= 0 && index < current.length) {
128+
current = current[index];
129+
} else {
130+
return null;
131+
}
132+
} catch (e) {
133+
return null;
134+
}
135+
} else {
136+
return null;
137+
}
103138
}
104139
}
105-
return current;
140+
if (current == null) {
141+
return "null";
142+
} else {
143+
return current;
144+
}
106145
}
107146

108147
Map<String, dynamic> _applyDataToTemplate(
@@ -218,8 +257,10 @@ class StacDynamicViewParser extends StacParser<StacDynamicView> {
218257
// Extract the value from the data
219258
final dataValue = _extractNestedData(data, keys);
220259

221-
processedValue =
222-
processedValue.replaceAll(placeholder, dataValue.toString());
260+
if (dataValue != null) {
261+
processedValue = processedValue.replaceAll(
262+
placeholder, dataValue.toString());
263+
}
223264
}
224265

225266
template[key] = processedValue;

0 commit comments

Comments
 (0)