簡體   English   中英

參數類型“字符串函數(字符串)”不能分配給參數類型“FutureOr”<string> 功能(電話號碼)'</string>

[英]The argument type 'String Function(String)' can't be assigned to the parameter type 'FutureOr<String> Function(PhoneNumber)'

我收到錯誤參數類型

'String Function(String)' 不能分配給參數類型'FutureOr Function(PhoneNumber)'

當我嘗試驗證代碼中的電話號碼時。 我正在嘗試使用 IntlPhoneField.validator 來驗證在電話字段中輸入的電話號碼,但每次它都會拋出此錯誤,我看不出問題出在哪里。 我正在使用 flutter 3.1.0。 請協助。 這是我的代碼。

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:intl_phone_field/country_picker_dialog.dart';
import 'package:intl_phone_field/intl_phone_field.dart';
import 'package:intl_phone_field/phone_number.dart';

class PhoneFieldWidget extends StatelessWidget {
  const PhoneFieldWidget(
      {Key key,
      this.onSaved,
      this.onChanged,
      this.validator,
      this.initialValue,
      this.hintText,
      this.errorText,
      this.labelText,
      this.obscureText,
      this.suffixIcon,
      this.isFirst,
      this.isLast,
      this.style,
      this.textAlign,
      this.suffix,
      this.initialCountryCode,
      this.countries})
      : super(key: key);

  final FormFieldSetter<PhoneNumber> onSaved;
  final ValueChanged<PhoneNumber> onChanged;
  final FormFieldValidator<String> validator;
  final String initialValue;
  final String hintText;
  final String errorText;
  final TextAlign textAlign;
  final String labelText;
  final TextStyle style;
  final bool obscureText;
  final String initialCountryCode;
  final List<String> countries;
  final bool isFirst;
  final bool isLast;
  final Widget suffixIcon;
  final Widget suffix;

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(top: 20, bottom: 14, left: 20, right: 20),
      margin: EdgeInsets.only(left: 20, right: 20, top: topMargin, bottom: bottomMargin),
      decoration: BoxDecoration(
          color: Get.theme.primaryColor,
          borderRadius: buildBorderRadius,
          boxShadow: [
            BoxShadow(color: Get.theme.focusColor.withOpacity(0.1), blurRadius: 10, offset: Offset(0, 5)),
          ],
          border: Border.all(color: Get.theme.focusColor.withOpacity(0.05))),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Text(
            labelText ?? "",
            style: Get.textTheme.bodyText1,
            textAlign: textAlign ?? TextAlign.start,
          ),
          IntlPhoneField(
              key: key,
              onSaved: onSaved,
              onChanged: onChanged,
              validator: validator,
              initialValue: initialValue ?? '',
              initialCountryCode: initialCountryCode ?? 'DE',
              showDropdownIcon: false,
              pickerDialogStyle: PickerDialogStyle(countryNameStyle: Get.textTheme.bodyText2),
              style: style ?? Get.textTheme.bodyText2,
              textAlign: textAlign ?? TextAlign.start,
              decoration: InputDecoration(
                hintText: hintText ?? '',
                hintStyle: Get.textTheme.caption,
                floatingLabelBehavior: FloatingLabelBehavior.never,
                contentPadding: EdgeInsets.all(0),
                border: OutlineInputBorder(borderSide: BorderSide.none),
                focusedBorder: OutlineInputBorder(borderSide: BorderSide.none),
                enabledBorder: OutlineInputBorder(borderSide: BorderSide.none),
                suffixIcon: suffixIcon,
                suffix: suffix,
                errorText: errorText,
              )),
        ],
      ),
    );
  }

錯誤指向這一行。

在此處輸入圖像描述

IntlPhoneFieldvalidator定義如下。 嘗試

 final FutureOr<String?> Function(PhoneNumber?)? validator;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM