无法使用静态访问访问 Flutter 实例成员“{0}" [英] Flutter Instance member ‘{0}’ can’t be accessed using static access
问题描述
我正在将变量从一个活动传递到另一个活动,但收到错误无法使用静态访问访问实例成员‘纬度’",我需要在该块中对其进行转换,以便我可以将其分配给静态 URL.
I am passing variables from one activity to another in flutter but getting the error "Instance member ‘latitude’ can’t be accessed using static access" I need it converted in that block so I can assign it to a static URL.
class Xsecond extends StatefulWidget {
final double latitude;
final double longitude;
Xsecond(this.latitude, this.longitude, {Key key}): super(key: key);
@override
_Xsecond createState() => _Xsecond();
}
class _Xsecond extends State<Xsecond> {
static String lat = Xsecond.latitude.toString(); // Error: Instance member ‘latitude’ can’t be accessed using static access
...
关注
...
String url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=${lat},$lng&radius=$radius&type=restaurant&key=$api';
...
推荐答案
在您的代码中,纬度和经度都被定义为非静态的,即实例变量.这意味着它们只能使用类实例调用.
In your code both latitude and longitude are defined as non-static i.e. are instance variables. Which means they can only be called using a class instance.
class _Xsecond extends State<Xsecond> {
final xsecond = Xsecond();
static String lat = xsecond.latitude.toString();
...
请阅读任何面向对象编程语言的基础知识,例如Dart,Java,C++
Please read the basics of any Object Oriented Programming language e.g. Dart, java, C++
但是,在您的上下文中,第一类是您的 StatefullWidget.因此,您可以通过状态类的 widget
字段访问它.
However, in your context the first class is your StatefullWidget. So you can access that by the widget
field of your state class.
修正:
class _Xsecond extends State<Xsecond> {
static String lat = widget.latitude.toString();
...
这篇关于无法使用静态访问访问 Flutter 实例成员“{0}"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!