在 null 上调用了 getter 'microsecondsSinceEpoch'

时间:2021-03-17 22:09:39

标签: flutter dart

我试图通过 Visibility 包装 TextFormField 小部件并发现此错误:

======== Exception caught by widgets library =======================================================
The following NoSuchMethodError was thrown building UserPage(dirty, dependencies: [_LocalizationsScope-[GlobalKey#f23fa], _InheritedTheme], state: _UserPageState#df216):
The getter 'microsecondsSinceEpoch' was called on null.
Receiver: null
Tried calling: microsecondsSinceEpoch

The relevant error-causing widget was: 
  UserPage file:///Users/mahmoudalharoon/Desktop/InterViewProjects/Mobile%20Technologies/mttest/lib/page/home_page.dart:44:37
When the exception was thrown, this was the stack: 
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
#1      DateTime.difference (dart:core-patch/date_patch.dart:201:54)
#2      _UserPageState.buildUsers (package:mttest/page/user_page.dart:161:33)
#3      _UserPageState.build (package:mttest/page/user_page.dart:46:15)
#4      StatefulElement.build (package:flutter/src/widgets/framework.dart:4684:27)

这是下面的代码一:

Visibility(
            visible:
                (DateTime.now().difference(user.dateOfBirth).inDays / 365) >=
                    18,
            child: CustomTextFormField(
              onChanged: (passNo) => setState(
                () => user = user.copy(passportNumber: passNo),
              ),
              userValue: user.passportNumber,
              userFLName: 'Passport Number',
              keyBoardType: TextInputType.number,
            ),
          ),

和用户类看起来像下面的代码:

class User {
  final String id;
  final String imei;
  final String firstName;
  final String lastName;
  final DateTime dateOfBirth;
  final String imagePath;
  final String passportNumber;
  final String userEmail;

  const User({
    this.id = '',
    this.imei = '',
    this.firstName = '',
    this.lastName = '',
    this.dateOfBirth,
    this.imagePath = '',
    this.passportNumber = '',
    this.userEmail = '',
  });

  User copy({
    String id,
    String imei,
    String firstName,
    String lastName,
    DateTime dateOfBirth,
    String imagePath,
    String passportNumber,
    String userEmail,
  }) =>
      User(
        id: id ?? this.id,
        imei: imei ?? this.imei,
        firstName: firstName ?? this.firstName,
        lastName: lastName ?? this.lastName,
        dateOfBirth: dateOfBirth ?? this.dateOfBirth,
        imagePath: imagePath ?? this.imagePath,
        passportNumber: passportNumber ?? this.passportNumber,
        userEmail: userEmail ?? this.userEmail,
      );

  static User fromJson(Map<String, dynamic> json) => User(
        id: json['id'],
        imei: json['imei'],
        firstName: json['name'],
        lastName: json['lastName'],
        dateOfBirth: DateTime.tryParse(json['dateOfBirth']),
        imagePath: json['imagePath'],
        passportNumber: json['passportNumber'],
        userEmail: json['userEmail'],
      );

  Map<String, dynamic> toJson() => {
        'id': id,
        'imei': imei,
        'name': firstName,
        'lastName': lastName,
        'dateOfBirth': dateOfBirth.toIso8601String(),
        'imagePath': imagePath,
        'passportNumber': passportNumber,
        'userEmail': userEmail,
      };

  @override
  String toString() => 'User{id: $id, name: $firstName}';
}

那么我需要在 18 岁以下时隐藏或将 Visible 设置为 false 到 Passport 文本和文本表单..

请看下图:

enter image description here

因为我试图在 How to make age validation in flutter 中使用以下答案:

bool isAdult2(String birthDateString) {
    String datePattern = "dd-MM-yyyy";

    // Current time - at this moment
    DateTime today = DateTime.now();

    // Parsed date to check
    DateTime birthDate = DateFormat(datePattern).parse(birthDateString);

    // Date to check but moved 18 years ahead
    DateTime adultDate = DateTime(
      birthDate.year + 18,
      birthDate.month,
      birthDate.day,
    );

    return adultDate.isBefore(adultDate);
  }

然后将可见属性更改为:

visible: isAdult2(user.dateOfBirth.toString()) ? false : true,

也不起作用,因为我发现了以下错误:

======== Exception caught by widgets library =======================================================
The following FormatException was thrown building UserPage(dirty, dependencies: [_LocalizationsScope-[GlobalKey#d2a75], _InheritedTheme], state: _UserPageState#f8143):
Trying to read dd from null at position 0

The relevant error-causing widget was: 
  UserPage file:///Users/mahmoudalharoon/Desktop/InterViewProjects/Mobile%20Technologies/mttest/lib/page/home_page.dart:44:37
When the exception was thrown, this was the stack: 
#0      _DateFormatField.throwFormatException (package:intl/src/intl/date_format_field.dart:87:5)
#1      _DateFormatPatternField.parseField (package:intl/src/intl/date_format_field.dart:337:7)
#2      _DateFormatPatternField.parse (package:intl/src/intl/date_format_field.dart:250:5)
#3      DateFormat._parse (package:intl/src/intl/date_format.dart:374:13)
#4      DateFormat.parse (package:intl/src/intl/date_format.dart:303:7)
...

0 个答案:

没有答案