Class 省道颤振:为什么终端输出这些线?

Class 省道颤振:为什么终端输出这些线?,class,flutter,dart,output,Class,Flutter,Dart,Output,达特新手,对我宽容点 我开始研究API,培训的一部分是从API中获取数据。 我创建了一个单独的类来获取数据,并在get time页面中导入它,以便在initState中初始化它。 这是我的班级: import 'dart:convert'; import 'package:http/http.dart'; class WorldTimeClass { String flag; // this is the link to a .png flag String url; // this

达特新手,对我宽容点

我开始研究API,培训的一部分是从API中获取数据。 我创建了一个单独的类来获取数据,并在get time页面中导入它,以便在
initState
中初始化它。 这是我的班级:

import 'dart:convert';
import 'package:http/http.dart';


class WorldTimeClass {
  String flag; // this is the link to a .png flag
  String url; // this is the suffix of the location e.g.: Europe/Berlin
  String time; // this is the time format to be displayed to the user
  String location; // this is the location, say Berlin

  WorldTimeClass({this.flag, this.url, this.time, this.location}); // class object

  Future<String> getData() async { // async method to fetch the data
    Response load = await get('http://worldtimeapi.org/api/timezone/$url');
    Map x(){if(load.statusCode == 200){ // if all goes well, fetch content from API
      print(load.statusCode); // this is so I can see that the operation is successful

    Map map = jsonDecode(load.body); // converting String to MAP
    return map;} // returning Map Object
    else{ // in case of error
      print('No Access');
      return {1:'NoAccess.'};} // had to return a Map Object since I declared Future<Map>
   }

    String datetime = x()['utc_datetime']; // fetching datetime String
    String offsetUTC = x()['utc_offset']; // fetching offset, e.g. +01:00
    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(); // returning String to be displayed to user

  }

}
问题:在终端输出中,我得到以下结果:

Restarted application in 1 174ms.
I/flutter (24151): initState..
I/flutter (24151): Instance of 'Future<String>'
I/flutter (24151): 200
I/flutter (24151): 200
在1中重新启动应用程序 174毫秒。
I/颤振(24151):初始状态。。
I/flatter(24151):“未来”的实例
I/颤振(24151):200
I/颤振(24151):200
问题:

  • 为什么我两次得到代码:200
  • 为什么我会得到“未来”的
    实例
    以及如何摆脱它
  • 如何在我的终端中打印时间字符串变量
  • 为什么我会得到“未来”的例子,以及如何摆脱它

    如何在我的终端中打印时间字符串变量

    试着用这种方法从
    'Future'

    为什么我两次得到代码:200

    您的状态代码正在打印两次,因为如果您在此之外打印日志,您的
    Map x(){}
    会被调用两次,它将只打印一次

    试着用这种方式来争取时间

    创建一个pojo类来解析json

    class TimeData {
      String abbreviation;
      String client_ip;
      String datetime;
      int day_of_week;
      int day_of_year;
      bool dst;
      int dst_offset;
      int raw_offset;
      String timezone;
      int unixtime;
      String utc_datetime;
      String utc_offset;
      int week_number;
    
      TimeData(
          {this.abbreviation,
          this.client_ip,
          this.datetime,
          this.day_of_week,
          this.day_of_year,
          this.dst,
          this.dst_offset,
          this.raw_offset,
          this.timezone,
          this.unixtime,
          this.utc_datetime,
          this.utc_offset,
          this.week_number});
    
      factory TimeData.fromJson(Map<String, dynamic> json) {
        return TimeData(
          abbreviation: json['abbreviation'],
          client_ip: json['client_ip'],
          datetime: json['datetime'],
          day_of_week: json['day_of_week'],
          day_of_year: json['day_of_year'],
          dst: json['dst'],
          dst_offset: json['dst_offset'],
          raw_offset: json['raw_offset'],
          timezone: json['timezone'],
          unixtime: json['unixtime'],
          utc_datetime: json['utc_datetime'],
          utc_offset: json['utc_offset'],
          week_number: json['week_number'],
        );
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['abbreviation'] = this.abbreviation;
        data['client_ip'] = this.client_ip;
        data['datetime'] = this.datetime;
        data['day_of_week'] = this.day_of_week;
        data['day_of_year'] = this.day_of_year;
        data['dst'] = this.dst;
        data['dst_offset'] = this.dst_offset;
        data['raw_offset'] = this.raw_offset;
        data['timezone'] = this.timezone;
        data['unixtime'] = this.unixtime;
        data['utc_datetime'] = this.utc_datetime;
        data['utc_offset'] = this.utc_offset;
        data['week_number'] = this.week_number;
    
        return data;
      }
    }
    
    现在像这样使用

     @override
      void initState() {
        print('initState..');
        super.initState();
        provisionalFunc().then((timeData) => {print(timeData)});
      }
    
    输出


    因为
    字符串datetime=x()['utc_datetime'];//获取日期时间字符串字符串偏移量utc=x()['utc_offset']
    x()
    twice@dev-aentgs谢谢你,有没有办法叫它一次,但要取两把钥匙?例如,
    utc\u datetime
    utc\u offset
    。是的,您已经有了结果,请将其保存在
    映射变量中
    并访问。像
    Map results=x();字符串datetime=results['utc_datetime'];字符串offsetUTC=结果['utc_offset']一个调用正在获取所有数据,只需保存并访问它@MehdiRezzagHebla
    
    class TimeData {
      String abbreviation;
      String client_ip;
      String datetime;
      int day_of_week;
      int day_of_year;
      bool dst;
      int dst_offset;
      int raw_offset;
      String timezone;
      int unixtime;
      String utc_datetime;
      String utc_offset;
      int week_number;
    
      TimeData(
          {this.abbreviation,
          this.client_ip,
          this.datetime,
          this.day_of_week,
          this.day_of_year,
          this.dst,
          this.dst_offset,
          this.raw_offset,
          this.timezone,
          this.unixtime,
          this.utc_datetime,
          this.utc_offset,
          this.week_number});
    
      factory TimeData.fromJson(Map<String, dynamic> json) {
        return TimeData(
          abbreviation: json['abbreviation'],
          client_ip: json['client_ip'],
          datetime: json['datetime'],
          day_of_week: json['day_of_week'],
          day_of_year: json['day_of_year'],
          dst: json['dst'],
          dst_offset: json['dst_offset'],
          raw_offset: json['raw_offset'],
          timezone: json['timezone'],
          unixtime: json['unixtime'],
          utc_datetime: json['utc_datetime'],
          utc_offset: json['utc_offset'],
          week_number: json['week_number'],
        );
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['abbreviation'] = this.abbreviation;
        data['client_ip'] = this.client_ip;
        data['datetime'] = this.datetime;
        data['day_of_week'] = this.day_of_week;
        data['day_of_year'] = this.day_of_year;
        data['dst'] = this.dst;
        data['dst_offset'] = this.dst_offset;
        data['raw_offset'] = this.raw_offset;
        data['timezone'] = this.timezone;
        data['unixtime'] = this.unixtime;
        data['utc_datetime'] = this.utc_datetime;
        data['utc_offset'] = this.utc_offset;
        data['week_number'] = this.week_number;
    
        return data;
      }
    }
    
    Future<String> getData() async {
        // async method to fetch the data
        Response load = await get('http://worldtimeapi.org/api/timezone/$url');
        print(load.statusCode);
    
        if (load.statusCode == 200) {
          var timeDate = TimeData.fromJson(json.decode(load.body));
          String datetime = timeDate.utc_datetime; // fetching datetime String
          String offsetUTC = timeDate.utc_offset; // fetching offset, e.g. +01:00
          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();
        }else{
          return "No access";
        }
    
        // returning String to be displayed to user
      }
    
     @override
      void initState() {
        print('initState..');
        super.initState();
        provisionalFunc().then((timeData) => {print(timeData)});
      }