updateVariablesProvider function

void updateVariablesProvider(
  1. WidgetRef ref
)

Implementation

void updateVariablesProvider(WidgetRef ref) {
  // Reset the rolesProvider and typesProvider.

  // ref.read(rolesProvider.notifier).state = {};
  // ref.read(typesProvider.notifier).state = {};

  // Get the most recent vars information from glimpse and update the
  // information in roles provider and types provider.

  String stdout = ref.watch(stdoutProvider);
  List<VariableInfo> vars = extractVariables(stdout);

  // 20241213 gjw Remove this for now. It was used for determining IGNORE roles
  // but due to an issue with that (see other comments) it is no longer being
  // done.

  // List<String> highVars = extractLargeFactors(stdout);

  // When a new row is added after transformation, initialise its role and
  // update the role of the old variable.

  for (var column in vars) {
    // Update roles.

    if (!ref.read(rolesProvider.notifier).state.containsKey(column.name)) {
      if (isTransformedVar(column.name)) {
        // Update the old variable's role.

        ref.read(rolesProvider.notifier).state[column.name] = Role.input;

        // Update the new variable's role.

        ref.read(rolesProvider.notifier).state[getOriginal(column.name)] =
            Role.ignoreAfterTransformed;
      } else {
        // 20250108 gjw Keep this debugPrint(). If we add a new transformation
        // (as we did for TFC) and the developer has not added it to the known
        // prefixes above then they need to be informed to do so. A NULL
        // exception is generated otherwise.

        debugPrint('** ERROR: Unidentified variable: ${column.name}.\n'
            '** ERROR: Please add it to the list in '
            'utils/update_roles_provider.dart');
      }
    }

    // Update the types.

    if (!ref.read(typesProvider.notifier).state.containsKey(column.name)) {
      ref.read(typesProvider.notifier).state[column.name] =
          isNumeric(column.type) ? Type.numeric : Type.categoric;
    }

    // 20241213 gjw Remove this heuristic for now. It is causing the IGNORE of
    // country in the PROTEIN dataset to be retained even if I change it to
    // INPUT/RISK/IDENT. Not if I change it to TARGET as per the test below. It
    // should actually be IDENT or TARGET.

    // 20241213 gjw This is repeated code from dataset/display.dart - we should
    // avoid repeated code as it is harder to maintain as I have just
    // demonstrated - I had to find this in two places to fix :-)

    // for (var highVar in highVars) {
    //   if (ref.read(rolesProvider.notifier).state[highVar] != Role.target) {
    //     ref.read(rolesProvider.notifier).state[highVar] = Role.ignore;
    //   }
    // }
  }
}