如果目标页面没有上下文,如何跨页面更改变量?

How do I change the variable from across page if the target page has no context?

'databaseFunctions.dart'

String path = 'path'     // a variable needed to be modified across page
Future queryALL() async {
    Database db = await openDatabase(path);
    return await db.query('all'); }

'main.dart'

// inside a stateful widget 
DropdownButton(
  value: currentValue,
  onChanged: (String newValue) {
        setState(() {
          currentValue = newValue;
          >> path = newValue << ;}}  // How can I accomplish this?

'few other pages'
// call queryALL() to build dataTable

Provider,Navigator 没有工作,因为 var x 所在的页面没有 Widget,因此没有任何上下文的显式入口。 'import' 不起作用,因为它只初始化 var x。 有什么想法吗?

我在这种情况下使用状态管理 Getx。像这样导入

dependencies:
  get: ^3.8.0

像这样定义控制器

class DatabaseController extends GetxController{
    RxString path = 'path'.obs;
    Future queryALL() async {
        Database db = await openDatabase(path);
        return await db.query('all'); 
    }
}

如果您要在任何地方使用此控制器,您应该在程序启动时启动。我建议您在 main.dart

中进行
DatabaseController dbController = Get.put(DatabaseController());

那么你总是可以像这样访问这个控制器

DatabaseController dbController = Get.find();

你只需要这样调用

DropdownButton(
  value: currentValue,
  onChanged: (String newValue) {
      setState(() {
          currentValue = newValue;
          dbController.path.value = newValue;
          dbController.queryAll();
      }
  }