forceRebuild static method

void forceRebuild(
  1. String id
)

If you have multiple I18n widgets you can call this method to force specific I18n widgets to rebuild. First, give it an id (and not a key), for example: I18n(id:"myWidget", child: ...).

Then, whenever you need to rebuild it: I18n.forceRebuild("myWidget"). Note this is a hack. It is not recommended that you have multiple I18n widgets in your widget tree.

Note: The id must be a literal, const String, because GlobalObjectKey creates the key from the the id identity.

Implementation

static void forceRebuild(String id) {
  var key = GlobalObjectKey<_I18nState>(id);
  var state = key.currentState;

  if (state == null)
    throw TranslationsException("Can't find a `I18n` widget "
        "with id=`$id` up in the tree.");

  state._rebuildAllChildren();

  // ignore: invalid_use_of_protected_member
  state.setState(() {});
}