I want to add some content at the end of first children. Below is the code sample.
String content= '''GetPage(
name: Routes.user,
page: () => UserScreen(),
binding: UserBindings(),
middlewares: [ConnectivityMiddleware(), AuthMiddleware()],
transition: Transition.fadeIn,
children: [
GetPage(
name: Routes.userDetails,
page: () => UserDetailsScreen(),
binding: UserDetailsBindings(),
middlewares: [ConnectivityMiddleware(), AuthMiddleware()],
transition: Transition.fadeIn,
children: [
GetPage(
name: Routes.userAddress,
page: () => UserAddressScreen(),
binding: UserAddressBindings(),
middlewares: [ConnectivityMiddleware(), AuthMiddleware()],
transition: Transition.fadeIn),
],
),
],
)''';
Content to add are below.
String contentToAdd = '''GetPage(
name: Routes.userRole,
page: () => UserRoleScreen(),
binding: UserRoleBindings(),
middlewares: [ConnectivityMiddleware(), AuthMiddleware()],
transition: Transition.fadeIn)''';
Desire output:-
String output = '''GetPage(
name: Routes.user,
page: () => UserScreen(),
binding: UserBindings(),
middlewares: [ConnectivityMiddleware(), AuthMiddleware()],
transition: Transition.fadeIn,
children: [
GetPage(
name: Routes.userDetails,
page: () => UserDetailsScreen(),
binding: UserDetailsBindings(),
middlewares: [ConnectivityMiddleware(), AuthMiddleware()],
transition: Transition.fadeIn,
children: [
GetPage(
name: Routes.userAddress,
page: () => UserAddressScreen(),
binding: UserAddressBindings(),
middlewares: [ConnectivityMiddleware(), AuthMiddleware()],
transition: Transition.fadeIn),
],
),
GetPage(
name: Routes.userRole,
page: () => UserRoleScreen(),
binding: UserRoleBindings(),
middlewares: [ConnectivityMiddleware(), AuthMiddleware()],
transition: Transition.fadeIn),
],
)''';
What I have tried is below. But it is only working when there is only one children. If more than one children is coming like above, code is not working.
if (content.contains('children: [')) {
final pattern = RegExp(r'(s*children:s*[.*?)(s*]s*))', dotAll: true);
final updatedContent = content.replaceAllMapped(
pattern,
(match) {
final childrenPart = match.group(1)!;
final closingPart = match.group(2)!;
return '$childrenPartn$contentToAdd$closingPart';
},
);
}
Please help me to resolve this issue. Child can be nested n number of time but I have to always add to first children only.