简体   繁体   English

如何从 dart 中的另一个文件访问变量

[英]How can I access a variable from another file in dart

I am trying to access a variable called _country from another file in dart, but an error occured: The getter '_country' isn't defined for the type 'CurrentCountry'.我试图从 dart 中的另一个文件访问名为_country的变量,但发生错误: The getter '_country' isn't defined for the type 'CurrentCountry'.

Here's the code where the variable I want to access is(in another file):这是我要访问的变量所在的代码(在另一个文件中):

class CurrentCountry {
  static String _country = 'All';
}

And here's the code where I want to access the variable:这是我要访问变量的代码:

  Future<Map<String, dynamic>> fetchWorldData() async {
    Response activeResponse = await get(Uri.parse(
        'https://disease.sh/v3/covid-19/countries/${CurrentCountry._country}'));
    return json.decode(activeResponse.body);
  }

If you can help me, I will be very grateful.如果你能帮助我,我将不胜感激。

You should remove the underscore of the variable.您应该删除变量的下划线。

If an identifier starts with an underscore (_), it's private to its library如果标识符以下划线 (_) 开头,则它对其库是私有的

Reference here: Dart language Important concepts参考这里:Dart语言重要概念

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter 从另一个 dart 文件访问一个变量 - Flutter access a variable from another dart file 如何在颤振中将变量从一个 .dart 文件导入到另一个文件 - How can I import a variable from one .dart file to another in flutter 如何从另一个 dart 文件访问值? - How to access values from another dart file? Flutter - 在 Dart 中,如何从一个类访问一个变量和函数到另一个类(Diff 文件)? - Flutter - In Dart, How to access a variable and function from a class to another class(Diff file)? 如何访问和更改在 FLUTTER/DART 的另一个 class 中声明的字符串变量的值 - How can i access and change value of string variable which is declared in another class in FLUTTER/DART 我可以从 dart 的子小部件中的父小部件访问变量吗? - Can I access a variable from the parent widget in a child widget in dart? 我将如何访问另一个 dart 文件中的 ScrollController? - How would I access the ScrollController in another dart file? 如何从 dart 中的不同 class 访问公共 static 变量? - How can I access a public static variable from a different class in dart? 如何从一个 dart 文件访问另一个方法? - How to access a method from one dart file to another? 如何在 flutter 中从一个 dart 文件访问 static 列表到另一个文件? - How to access the static list from one dart file to another in flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM