Skip to content

Conversation

@dixita0607
Copy link
Contributor

A follow up PR resolving PR comments on #12673 by Gemini code assist

Presubmit checklist

  • If you are unwilling, or unable, to sign the CLA, even for a tiny, one-word PR, please file an issue instead of a PR.
  • If this PR is not meant to land until a future stable release, mark it as draft with an explanation.
  • This PR follows the Google Developer Documentation Style Guidelines—for example, it doesn't use i.e. or e.g., and it avoids I and we (first-person pronouns).
  • This PR uses semantic line breaks
    of 80 characters or fewer.

@dixita0607 dixita0607 requested review from a team and sfshaza2 as code owners November 13, 2025 16:43
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @dixita0607, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the resilience of the website's theme loading mechanism. By encapsulating the localStorage access within a try-catch block, the system can now gracefully handle scenarios where localStorage is not available or restricted, preventing errors and ensuring that the site can still render with a default theme.

Highlights

  • Robustness: Implemented a try-catch block around the JavaScript code that accesses localStorage to retrieve theme preferences.
  • Error Handling: This change prevents potential runtime errors in environments where localStorage might be unavailable or restricted, ensuring a more stable user experience.
  • Theme Fallback: If localStorage is inaccessible, the theme loading logic will now gracefully fail without breaking, allowing the site to fall back to its default theme.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly adds a try-catch block to handle potential errors when accessing localStorage for theme settings. This is a good improvement for robustness.

I've added one suggestion to slightly optimize the script by moving a variable declaration into the scope where it's used.

Additionally, I noticed that localStorage is also accessed in site/lib/src/components/layout/theme_switcher.dart when setting the theme, which could also benefit from being wrapped in a try-catch block. This is outside the scope of this PR, but it would be great to address it in a follow-up to ensure consistent error handling across the theme-related logic.

Comment on lines +175 to +185
const prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)');
const storedTheme = window.localStorage.getItem('theme') ?? 'light-mode';
if (storedTheme === 'auto-mode') {
document.body.classList.add(
'auto-mode',
prefersDarkMode.matches ? 'dark-mode' : 'light-mode',
);
} else {
document.body.classList.add(storedTheme);
const storedTheme = window.localStorage.getItem('theme') ?? 'light-mode';
if (storedTheme === 'auto-mode') {
document.body.classList.add(
'auto-mode',
prefersDarkMode.matches ? 'dark-mode' : 'light-mode',
);
} else {
document.body.classList.add(storedTheme);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For a minor performance improvement and to scope variables more tightly, you can move the prefersDarkMode declaration inside the if (storedTheme === 'auto-mode') block, since it's only used there. This avoids an unnecessary call to window.matchMedia when the theme is not set to 'auto-mode'.

Suggested change
const prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)');
const storedTheme = window.localStorage.getItem('theme') ?? 'light-mode';
if (storedTheme === 'auto-mode') {
document.body.classList.add(
'auto-mode',
prefersDarkMode.matches ? 'dark-mode' : 'light-mode',
);
} else {
document.body.classList.add(storedTheme);
const storedTheme = window.localStorage.getItem('theme') ?? 'light-mode';
if (storedTheme === 'auto-mode') {
document.body.classList.add(
'auto-mode',
prefersDarkMode.matches ? 'dark-mode' : 'light-mode',
);
} else {
document.body.classList.add(storedTheme);
}
const storedTheme = window.localStorage.getItem('theme') ?? 'light-mode';
if (storedTheme === 'auto-mode') {
const prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)');
document.body.classList.add(
'auto-mode',
prefersDarkMode.matches ? 'dark-mode' : 'light-mode',
);
} else {
document.body.classList.add(storedTheme);
}

@dixita0607
Copy link
Contributor Author

@parlough While making this change, I notice three more file not having a try catch while accessing localStorage

Should I go ahead with the same change in them too?

Also I think if this is okay, I can make a PR on dart's website repo too.

@parlough
Copy link
Member

parlough commented Nov 13, 2025

While making this change, I notice three more files not having a try catch while accessing localStorage.

Should I go ahead with the same change in them too?

Thanks for checking for other locations @dixita0607! I think it makes sense to make those other usages more resilient as well, so sure. Let me know if you have any questions about how to handle any of those cases.

 - This is an extension of a PR comment recommended by Gemini code assist in another PR
 - For now, the catch blocks prints the errors if the app is running in the debug mode
'tab-save-$currentSaveKey',
currentSaveId,
);
try {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @parlough I added the changes in the rest of the files. For now I have added some print statements in debug mode. Please feel free to direct me if we need to perform other actions there.

Also, it'd be great if you can give tips on testing this.

@flutter-website-bot
Copy link
Collaborator

Visit the preview URL for this PR (updated for commit 412cb7c):

https://flutter-docs-prod--pr12682-12673-work-on-pr-comments-gv1od254.web.app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants