dumpRenderObjectTree method
Implementation
Future<RenderObjectTreeDumpResult?> dumpRenderObjectTree(
String? routePath, {
bool writeToFile = false,
bool printToConsole = false,
}) async {
String? renderObjectTreeString;
if (routePath == null || routePath == initialRoute) {
renderObjectTreeString = view.getRootRenderObject()?.toStringDeep();
} else {
final RouterLinkElement? routeLinkElement = view.getHybridRouterView(routePath);
renderObjectTreeString = routeLinkElement?.getRenderObjectTree();
}
if (renderObjectTreeString == null || renderObjectTreeString.isEmpty) {
return null;
}
String? savedFilePath;
if (writeToFile && (Platform.isMacOS || Platform.isWindows || Platform.isLinux)) {
try {
Directory? documentsDir;
if (Platform.isMacOS || Platform.isLinux) {
final String? home = Platform.environment['HOME'];
if (home != null && home.isNotEmpty) {
documentsDir = Directory('$home/Documents');
}
} else if (Platform.isWindows) {
final String? userProfile = Platform.environment['USERPROFILE'];
if (userProfile != null && userProfile.isNotEmpty) {
documentsDir = Directory('$userProfile\\Documents');
}
}
documentsDir ??= await getApplicationDocumentsDirectory();
final webfDebugDir = Directory('${documentsDir.path}${Platform.pathSeparator}WebF_Debug');
await webfDebugDir.create(recursive: true);
final timestamp = DateTime.now().toIso8601String().replaceAll(':', '-').replaceAll('.', '-');
final sanitizedRoute = (routePath ?? 'root').replaceAll(RegExp(r'[^a-zA-Z0-9_-]+'), '_');
final filename = 'render_tree_${sanitizedRoute}_$timestamp.txt';
final file = File('${webfDebugDir.path}${Platform.pathSeparator}$filename');
await file.writeAsString(renderObjectTreeString);
savedFilePath = file.path;
} catch (e) {
debugPrint('Failed to write render object tree to file: $e');
}
}
if (printToConsole) {
debugPrint(renderObjectTreeString);
}
return RenderObjectTreeDumpResult(
text: renderObjectTreeString,
savedFilePath: savedFilePath,
);
}