Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Small fix on Translations.get #564

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

jonasbadstuebner
Copy link

I got an error like this:

Flutter Error:  type '_Map<String, dynamic>' is not a subtype of type 'String?'

from Translations.get.
I had a JSON for translation like

{
  "error": {
    "unknown": "An unknown error occurred",
    "unauthenticated": "You need to log in first"
  }
}

etc.

My code was trying to translate like tr(errorString) where errorString was "error" and this led to the lookup in lib/src/translations.dart#L16 to try and put the resulting Map (

{
  "unknown": "An unknown error occurred",
  "unauthenticated": "You need to log in first"
}

) into a String?.

This PR fixes the problem and just gives [🌎 Easy Localization] [WARNING] Localization key [error] not found as expected.

I did not check in any other places, if this could happen. If an error occurs somewhere else, I will open a new PR.

By the way, when will you release the next time? The last release on pub.dev was 10 months ago...?

@bw-flagship
Copy link
Collaborator

@DrBu7cher Thanks for this PR, a very good idea! Maybe we should add a dedicated warning-log in this case, so the user knows what's wrong? Anyways, it would be important to have a unittest covering this, so it's not destroyed by a another change.

.fvm/fvm_config.json Outdated Show resolved Hide resolved
.fvm/flutter_sdk Outdated Show resolved Hide resolved
@@ -13,7 +13,8 @@ class Translations {

/// If we failed to find the key as a nested key, then fall back
/// to looking it up like normal.
returnValue ??= _translations?[key];
var maybeValue = _translations?[key];
Copy link
Collaborator

Choose a reason for hiding this comment

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

Now we do the lookup all the time, even if it's not needed because returnValue != null

@@ -13,7 +13,8 @@ class Translations {

/// If we failed to find the key as a nested key, then fall back
/// to looking it up like normal.
returnValue ??= _translations?[key];
var maybeValue = _translations?[key];
if (maybeValue is String) returnValue ??= maybeValue;
Copy link
Collaborator

Choose a reason for hiding this comment

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

we need a unittest for the new behavior

@jonasbadstuebner
Copy link
Author

Good ideas, I will come back to that.

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.

2 participants