getter 'dateOfBirth' 被调用为 null。接收者:null 尝试调用:dateOfBirth

时间:2021-03-19 10:04:22

标签: flutter dart

我试图通过设置状态值来使用日历小部件,但我发现以下错误:

The following NoSuchMethodError was thrown building CustomerForm(dirty, dependencies: [_LocalizationsScope-[GlobalKey#e6983], _InheritedTheme], state: CustomerFormState#fc7d8):
The getter 'dateOfBirth' was called on null.
Receiver: null
Tried calling: dateOfBirth

这是我设置状态的方式:

  Widget buildBirthday() => BirthdayWidget(
      birthday: customer.dateOfBirth,
      onChangedBirthday: (dateOfBirth) => setState(
            () => customer.dateOfBirth = dateOfBirth,
          ));

客户模型应该是这样的:

import 'package:regform/db/database_provider.dart';

class Customer {
  int id;
  int imei;
  String firstName;
  String lastName;
  DateTime dateOfBirth;
  String passportNo;
  String email;
  String customerPhoto;

  Customer({
    this.id,
    this.firstName,
    this.lastName,
    this.dateOfBirth,
    this.passportNo,
    this.email,
    this.customerPhoto,
  });

  Map<String, dynamic> toMap() {
    var map = <String, dynamic>{
      DatabaseProvider.COLUMN_IMEI: imei,
      DatabaseProvider.COLUMN_FIRST_NAME: firstName,
      DatabaseProvider.COLUMN_LAST_NAME: lastName,
      DatabaseProvider.COLUMN_DOB: dateOfBirth,
      DatabaseProvider.COLUMN_PASSPORT_NO: passportNo,
      DatabaseProvider.COLUMN_EMAIL: lastName,
      DatabaseProvider.COLUMN_CUSTOMER_PHOTO: customerPhoto,
    };

    if (id != null) {
      map[DatabaseProvider.COLUMN_ID] = id;
    }

    return map;
  }

  Customer.fromMap(Map<String, dynamic> map) {
    id = map[DatabaseProvider.COLUMN_ID];
    imei = map[DatabaseProvider.COLUMN_IMEI];
    firstName = map[DatabaseProvider.COLUMN_FIRST_NAME];
    lastName = map[DatabaseProvider.COLUMN_LAST_NAME];
    dateOfBirth = map[DatabaseProvider.COLUMN_DOB];
    passportNo = map[DatabaseProvider.COLUMN_PASSPORT_NO];
    email = map[DatabaseProvider.COLUMN_EMAIL];
    customerPhoto = map[DatabaseProvider.COLUMN_CUSTOMER_PHOTO];
  }
}

这是生日小部件:

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class BirthdayWidget extends StatefulWidget {
  final DateTime birthday;
  final ValueChanged<DateTime> onChangedBirthday;

  const BirthdayWidget({
    Key key,
    @required this.birthday,
    @required this.onChangedBirthday,
  }) : super(key: key);

  @override
  _BirthdayWidgetState createState() => _BirthdayWidgetState();
}

class _BirthdayWidgetState extends State<BirthdayWidget> {
  final controller = TextEditingController();
  final focusNode = FocusNode();

  @override
  void initState() {
    super.initState();

    setDate();
  }

  @override
  void didUpdateWidget(covariant BirthdayWidget oldWidget) {
    super.didUpdateWidget(oldWidget);

    setDate();
  }

  void setDate() => setState(() {
        controller.text = widget.birthday == null
            ? ''
            : DateFormat.yMd().format(widget.birthday);
      });

  @override
  Widget build(BuildContext context) => FocusBuilder(
        onChangeVisibility: (isVisible) {
          if (isVisible) {
            selectDate(context);
            //
          } else {
            FocusScope.of(context).requestFocus(FocusNode());
          }
        },
        focusNode: focusNode,
        builder: (hasFocus) => TextFormField(
          controller: controller,
          validator: (value) => value.isEmpty ? 'Is Required' : null,
          decoration: InputDecoration(
            prefixText: ' ',
            hintText: 'Your birthday',
            prefixIcon: Icon(Icons.calendar_today_rounded),
            border: OutlineInputBorder(),
          ),
        ),
      );

  Future selectDate(BuildContext context) async {
    final birthday = await showDatePicker(
      context: context,
      initialDate: widget.birthday ?? DateTime.now(),
      firstDate: DateTime(1950),
      lastDate: DateTime(2100),
    );

    if (birthday == null) return;

    widget.onChangedBirthday(birthday);
  }
}

class FocusBuilder extends StatefulWidget {
  final FocusNode focusNode;
  final Widget Function(bool hasFocus) builder;
  final ValueChanged<bool> onChangeVisibility;

  const FocusBuilder({
    @required this.focusNode,
    @required this.builder,
    @required this.onChangeVisibility,
    Key key,
  }) : super(key: key);

  @override
  _FocusBuilderState createState() => _FocusBuilderState();
}

class _FocusBuilderState extends State<FocusBuilder> {
  @override
  Widget build(BuildContext context) => GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () => widget.onChangeVisibility(true),
        child: Focus(
          focusNode: widget.focusNode,
          onFocusChange: widget.onChangeVisibility,
          child: widget.builder(widget.focusNode.hasFocus),
        ),
      );
}

这就是我发起客户课程的方式:

final Customer customer;

1 个答案:

答案 0 :(得分:1)

这是因为当您尝试访问其属性 customer 时,null 变量仍然是 dateofBirth。您需要先创建一个 Customer 对象并将其分配给变量。

final Customer customer = Customer();