将对象转换为可编码对象失败:“DateTime”的实例

时间:2021-05-12 20:13:12

标签: flutter dart

我正在使用一个需要日期来注册用户的 API,我收到了这个 Dio 异常: 将对象转换为可编码对象失败:“DateTime”实例

这是模型

import 'package:meta/meta.dart' show required;
// import 'package:intl/intl.dart';

class Customer {
  Customer({
    @required this.name,
    @required this.surname,
    @required this.phone,
    @required this.mail,
    @required this.password,
    @required this.acceptMarketing,
    this.image,
    @required this.genre,
    this.rfc,
    @required this.registeredAt,
  });

  final String name;
  final String surname;
  final List<String> phone;
  final String mail;
  final String password;
  final bool acceptMarketing;
  final Image image;
  final String genre;
  final String rfc;
  final DateTime registeredAt;

  factory Customer.fromJson(Map<String, dynamic> json) => Customer(
        name: json["Name"],
        surname: json["Surname"],
        phone: List<String>.from(json["Phone"].map((x) => x)),
        mail: json["Mail"],
        password: json["Password"],
        acceptMarketing: json["AcceptMarketing"],
        image: Image.fromJson(json["Image"]),
        genre: json["Genre"],
        rfc: json["RFC"],
        registeredAt: DateTime.parse(json["registered_at"]),
      );

  Map<String, dynamic> toJson() => {
        "Name": name,
        "Surname": surname,
        "Phone": List<dynamic>.from(phone.map((x) => x)),
        "Mail": mail,
        "Password": password,
        "AcceptMarketing": acceptMarketing,
        "Image": image.toJson(),
        "Genre": genre,
        "RFC": rfc,
        "registered_at": registeredAt.toIso8601String(),
      };
}

这是控制器

import 'package:abremexapp/app/data/models/customer.dart';
import 'package:abremexapp/app/data/repositories/register_repo.dart';
import 'package:abremexapp/app/routes/app_routes.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'locale_widgets/terms_page.dart';

class RegisterController extends GetxController {
  final RegisterRepo _registerRepo = Get.find<RegisterRepo>();

  RxString selected = 'hombre'.obs;
  RxBool marketing = true.obs;
  String _name = "", _surname = "", _mail = "", _password = "";
  List _phone = [];
  DateTime _registeredAt = DateTime.now().toUtc();

  @override
  void onReady() {
    super.onReady();
    print(_registeredAt);
  }

  void onNameChanged(String text) {
    _name = text;
  }

  void onSurnameChanged(String text) {
    _surname = text;
  }

  void onPhoneChanged(String text) {
    _phone = [text];
  }

  void onMailChanged(String text) {
    _mail = text;
  }

  void onPasswordChanged(String text) {
    _password = text;
  }

  void setValue(newValue) {
    selected.value = newValue;
    print(selected.value);
    update(['genre']);
  }

  void setMarketing(state) {
    marketing.value = state;
    print(marketing.value);
    update(['marketing']);
  }

  void navTC() {
    Get.to(() => TermsPage());
  }

  Future<void> submit() async {
    try {
      final Customer customer = await _registerRepo.newRegister(
        name: _name,
        surname: _surname,
        phone: _phone,
        mail: _mail,
        password: _password,
        acceptMarketing: marketing.value,
        genre: selected.value,
        registeredAt: _registeredAt,
      );
      print('User created  ${customer.registeredAt}');
      Get.toNamed(AppRoutes.LOGIN);
    } catch (e) {
      print(e);
      String message = "";
      if (e is DioError) {
        if (e.response != null) {
          message = e.response.statusMessage;
        } else {
          message = e.message;
        }
      }
      Get.dialog(
        AlertDialog(
          title: Text('Error'),
          content: Text(message),
          actions: [
            TextButton(
              onPressed: () {
                Get.back();
              },
              child: Text("ok"),
            )
          ],
        ),
      );
    }
  }
}

我该如何解决问题? 我必须将 DateTime 更改为 String 并尝试? 或者有没有办法对字符串进行编码?

0 个答案:

没有答案