2

null 安全性に問題があります。

以下のように、クラスに関数 onFieldSubmitted および onChanged を作成したい

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:rift/constants.dart';

class DateTextField extends StatelessWidget {

  final String dateComposition;
  final String dateCompositionHintText;
  final dynamic Function()? onFieldSubmitted;
  //onchanged function has to be determined if you want to automatically set the focus to another text field, see application on age_screen with the datetextfields
  final Function()? onChanged;
  //set the widget with its focusnode
  final FocusNode? focusNode;

それらを私のテキストフォームフィールドに入れたい

child: TextFormField(
            onFieldSubmitted: onFieldSubmitted!(),
            onChanged: onChanged!(),
            focusNode: focusNode!,

問題は、「null チェック演算子が null 値で使用されています」というエラーが発生し、関数 onFieldSubmitted および onChanged のバング (!) が原因で発生することです。このエラーは、レンダリングの問題を引き起こします [1]: https://i.stack.imgur.com/7ALjp.png [Rederflex][1] アプリで。

一方、バング (!) を削除すると、「関数は 'null' になる可能性があるため、無条件に呼び出すことはできません。null チェック ('!') を追加してみてください」というメッセージが表示されます。

基本的に私は今ループしており、それを解決する方法がわかりません

私はいくつかの助けを本当にうれしく思います

完全なコード:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:rift/constants.dart';

class DateTextField extends StatelessWidget {

  final String dateComposition;
  final String dateCompositionHintText;
  final dynamic Function()? onFieldSubmitted;
  //onchanged function has to be determined if you want to automatically set the focus to another text field, see application on age_screen with the datetextfields
  final Function()? onChanged;
  //set the widget with its focusnode
  final FocusNode? focusNode;

  DateTextField({required this.dateComposition, required this.dateCompositionHintText, this.onFieldSubmitted, this.onChanged, this.focusNode});

  //dateComposition can only have Day, Month, or Year as strings



  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        Text(
          dateComposition,
          style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600),
        ),
        SizedBox(height: 7),
        Container(
          margin: EdgeInsets.all(4),
          width: dateComposition == "Year"? 73: 55,
          child: TextFormField(
            onFieldSubmitted: onFieldSubmitted(),
            // onChanged: onChanged!(),
            focusNode: focusNode!,
            textAlign: TextAlign.center,
            //Keyboardtype for numbers
            keyboardType: TextInputType.number,
            //only numbers can be typed in
            inputFormatters: <TextInputFormatter>[
              FilteringTextInputFormatter.digitsOnly,
              LengthLimitingTextInputFormatter(dateComposition=="Year"? 4 : 2),
            ],
            style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600),
            autofocus: true,
            cursorColor: kPrimaryMagentaColor,
            decoration: InputDecoration(
              enabledBorder: OutlineInputBorder(
                borderSide: BorderSide(color: kTextIconColorDarkBlue),
                borderRadius: BorderRadius.circular(15),
              ),
              hintText: dateCompositionHintText,
              hintStyle: TextStyle(fontWeight: FontWeight.w600, fontSize: 18.0),
              contentPadding: EdgeInsets.only(
                left: 10,
                right: 10,
                top: 10,
                bottom: 10,
              ),
              focusedBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(15),
                borderSide: BorderSide(
                  color: kPrimaryMagentaColor,
                  width: 1.5,
                ),
              ),
            ),
          ),
        ),
      ],
    );
  }
}

エラーログ

═══════ Exception caught by widgets library ═══════════════════════════════════
The following _CastError was thrown building DateTextField(dirty):
Null check operator used on a null value

The relevant error-causing widget was
DateTextField
lib/…/age_screen/age_screen.dart:46
When the exception was thrown, this was the stack
#0      DateTextField.build
package:rift/…/age_screen/date_text_field.dart:36
#1      StatelessElement.build
package:flutter/…/widgets/framework.dart:4648
#2      ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4574
#3      Element.rebuild
package:flutter/…/widgets/framework.dart:4267
#4      ComponentElement._firstBuild
package:flutter/…/widgets/framework.dart:4553
...
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════
Null check operator used on a null value
The relevant error-causing widget was
DateTextField
lib/…/age_screen/age_screen.dart:57
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════
Null check operator used on a null value
The relevant error-causing widget was
DateTextField
lib/…/age_screen/age_screen.dart:68
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by rendering library ═════════════════════════════════
A RenderFlex overflowed by 299572 pixels on the right.
The relevant error-causing widget was
Row
lib/…/age_screen/age_screen.dart:41
The specific RenderFlex in question is: RenderFlex#70b39 relayoutBoundary=up2 OVERFLOWING
════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by rendering library ═════════════════════════════════
A RenderFlex overflowed by 99498 pixels on the bottom.
The relevant error-causing widget was
Column
lib/…/age_screen/age_screen.dart:25
════════════════════════════════════════════════════════════════════════════════
4

0 に答える 0