umutcan bağdu
umutcan bağdu

Reputation: 246

Unhandled Exception: type '(dynamic) => Time' is not a subtype of type '(String, dynamic) => MapEntry<dynamic, dynamic>' of 'transform'

Unhandled Exception: type '(dynamic) => Time' is not a subtype of type '(String, dynamic) => MapEntry<dynamic, dynamic>' of 'transform'

import 'package:wordle/core/data/time.dart';
import 'package:dio/dio.dart';

Future<Time?> getTime() async {
  var time;
  try {
    var response = await Dio().get(
        'https://www.timeapi.io/api/Time/current/zone?timeZone=Europe/Istanbul');
    if (response.statusCode == 200) {
      print(response.data.toString());
      //(Map<String, dynamic>)
      time = (response.data).map((e)  => Time.fromMap(e));
      print("data");
      print(time.minute);
      print(time.day);
      return time;
    } else {
      throw Exception("Veri gönderilmedi ${response.statusCode}");
    }
  } on DioError catch (e) {
    return Future.error(e.message);
  }
}

Time Class

factory Time.fromMap(Map<String, dynamic> json) => Time(
    year: json["year"],
    month: json["month"],
    day: json["day"],
    hour: json["hour"],
    minute: json["minute"],
    seconds: json["seconds"],
    milliSeconds: json["milliSeconds"],
    dateTime: DateTime.parse(json["dateTime"]),
    date: json["date"],
    time: json["time"],
    timeZone: json["timeZone"],
    dayOfWeek: json["dayOfWeek"],
    dstActive: json["dstActive"],
  );

Data = { "year": 2022, "month": 3, "day": 5, "hour": 18, "minute": 23, "seconds": 27, "milliSeconds": 765, "dateTime": "2022-03-05T18:23:27.7658753", "date": "03/05/2022", "time": "18:23", "timeZone": "Europe/Istanbul", "dayOfWeek": "Saturday", "dstActive": false }

Upvotes: 0

Views: 1228

Answers (1)

user18309290
user18309290

Reputation: 8370

dio will parse response to json object, so you only need to

time = Time.fromMap(response.data);

Upvotes: 2

Related Questions