Flutter 如何在类内部初始化类方法?
这是我的班级:Flutter 如何在类内部初始化类方法?,flutter,dart,methods,Flutter,Dart,Methods,这是我的班级: class WorldTimeClass { String flag; String url; String time; String location; WorldTimeClass({this.flag, this.url, this.time, this.location}); Future<String> getData() async { try{ Response load = await get('http
class WorldTimeClass {
String flag;
String url;
String time;
String location;
WorldTimeClass({this.flag, this.url, this.time, this.location});
Future<String> getData() async {
try{
Response load = await get('http://worldtimeapi.org/api/timezone/$url');
Map x(){if(load.statusCode == 200){
print(load.statusCode);
Map map = jsonDecode(load.body);
return map;}
else{
print('No Access');
return {1:'NoAccess.'};}
}
Map myMap = x();
String datetime = myMap['utc_datetime'];
String offsetUTC = myMap['utc_offset'];
DateTime dateTimeObjectConvert = DateTime.parse(datetime);
// Below converts the datetime string to a DateTime Object and then converts the UTC Offset to a substring only '01' out of +01:00 and then converts it to an int Object and then adds it to the DateTime Object as a Duration (hours);
dateTimeObjectConvert = dateTimeObjectConvert.add(Duration(hours: int.parse(offsetUTC.substring(1,3))));
return time = dateTimeObjectConvert.toString();
}
catch(e,s){
return 'Could not access time data from API.\nWe are sorry, please try again.\nError occured: $e';
}
}
var myString = getData().then((value) => value);
DateFormat pretty = DateFormat().add_jm().format(myString);
}
class世界时间类{
字符串标志;
字符串url;
串时间;
字符串位置;
WorldTimeClass({this.flag,this.url,this.time,this.location});
Future getData()异步{
试一试{
响应负载=等待获取('http://worldtimeapi.org/api/timezone/$url');
映射x(){if(load.statusCode==200){
打印(加载状态代码);
Map Map=jsonDecode(load.body);
返回映射;}
否则{
打印(“无访问权限”);
返回{1:'NoAccess.};}
}
Map myMap=x();
字符串datetime=myMap['utc_datetime'];
字符串offsetUTC=myMap['utc_offset'];
DateTime dateTimeObjectConvert=DateTime.parse(DateTime);
//下面将datetime字符串转换为datetime对象,然后将UTC偏移量转换为+01:00中仅为“01”的子字符串,然后将其转换为int对象,然后将其作为持续时间(小时)添加到datetime对象中;
dateTimeObjectConvert=dateTimeObjectConvert.add(持续时间(小时:int.parse(offsetUTC.substring(1,3)));
返回时间=dateTimeObjectConvert.toString();
}
渔获物(东、南){
return“无法从API访问时间数据。\n很抱歉,请重试。\n出现错误:$e”;
}
}
var myString=getData(),然后((value)=>value);
DateFormat pretty=DateFormat().add_jm().format(myString);
}
如何访问myString
并在类中执行它,以便在第二个方法pretty
中使用生成的String
对象?
此外,我需要了解以下例外情况的含义是什么?
在初始值设定项中只能访问静态成员。
要访问myString
变量,您必须执行以下操作之一:
WorldTimeClass
对象,并使用yourWorldClassObject.myString
static关键字
likestatic var myString
将其加入到static
成员中。这就是“在初始值设定项中只能访问静态成员。
”的全部内容。如果要访问该类的属性,必须创建该类的实例,或者使其静态以访问它们,而无需实例化对象。只需WorldTimeClass.myString
要访问
myString
变量,您必须执行以下操作之一:
WorldTimeClass
对象,并使用yourWorldClassObject.myString
static关键字
likestatic var myString
将其加入到static
成员中。这就是“在初始值设定项中只能访问静态成员。
”的全部内容。如果要访问该类的属性,必须创建该类的实例,或者使其静态以访问它们,而无需实例化对象。只需WorldTimeClass.myString
var myString = getData().then((value) => value);
DateFormat pretty = DateFormat().add_jm().format(myString);
因此,创建一个返回字符串的方法
,然后您所要做的就是调用该方法,它将为您提供字符串,并将上面的代码添加到该方法中:
String getDateFormat(){
var myString = getData().then((value) => value);
return DateFormat().add_jm().format(myString);
}
在初始值设定项中只能访问静态成员
这基本上意味着您不能直接在类声明下调用类的方法或访问特定类的属性
您在这两行中得到了错误:
var myString = getData().then((value) => value);
DateFormat pretty = DateFormat().add_jm().format(myString);
因此,创建一个返回字符串的方法
,然后您所要做的就是调用该方法,它将为您提供字符串,并将上面的代码添加到该方法中:
String getDateFormat(){
var myString = getData().then((value) => value);
return DateFormat().add_jm().format(myString);
}
谢谢你简洁明了的解释Pedro@Pedropmont谢谢你简洁明了的解释Pedro@Pedropmont谢谢你的回答和解决方案,非常感谢。谢谢你的回答和解决方案,非常感谢。