-1

i made a BlocBuilder and then this package can't run with null safety datetime_picker_formfield, i confuse how to fix the error and solve the error, this is the whole code of BlocBuilder:

      return BlocBuilder<InputFieldBloc, InputFieldBlocState>(
        bloc: inputFieldBloc,
        builder: (context, state) {
          return DateTimeField(
            format: DateFormat("dd-MM-yyyy"),
            initialValue: state.value,
            resetIcon: null,
            onChanged: (value) {
              inputFieldBloc.updateValue(value);
            },
            onShowPicker: (context, currentValue) async {
              await showCupertinoModalPopup(
                  context: context,
                  builder: (context) {
                    return BottomSheet(
                      builder: (context) => Column(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Container(
                            constraints: BoxConstraints(maxHeight: 200),
                            child: CupertinoDatePicker(
                              onDateTimeChanged: (value) {
                                inputFieldBloc.updateValue;
                                 },
                            ),
                          ),
                          TextButton(onPressed: () => Navigator.pop(context), child: 
                        Text('Ok')),
                        ],
                      ),
                      onClosing: () {},
                    );
                  });
              //setState(() {});
              return value;;
            },
            decoration: InputDecoration(
              labelText: labelText,
              prefixIcon: Icon(Icons.calendar_today),
              border: OutlineInputBorder(),
            ),
          );
        }
    );

the code that contains import from the dependencies is return DateTimeField can anyone solve and find the error? i need your help, in case if you need to see the full code:

class CuppertinoDatePickerBlocBuilder extends StatelessWidget {
  CuppertinoDatePickerBlocBuilder({
    Key? key, required this.inputFieldBloc, required this.labelText,}) : super(key: key);


  //var screen = MediaQuery.of(context).size;
    DateTime value = DateTime.now();
    final InputFieldBloc<DateTime?, dynamic> inputFieldBloc;
    final String labelText;

  @override
  Widget build(BuildContext context) {

    return BlocBuilder<InputFieldBloc, InputFieldBlocState>(
        bloc: inputFieldBloc,
        builder: (context, state) {
          return DateTimeField(
            format: DateFormat("dd-MM-yyyy"),
            initialValue: state.value,
            resetIcon: null,
            onChanged: (value) {
              inputFieldBloc.updateValue(value);
            },
            onShowPicker: (context, currentValue) async {
              await showCupertinoModalPopup(
                  context: context,
                  builder: (context) {
                    return BottomSheet(
                      builder: (context) => Column(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Container(
                            constraints: BoxConstraints(maxHeight: 200),
                            child: CupertinoDatePicker(
                              onDateTimeChanged: (value) {
                                inputFieldBloc.updateValue;
                                 },
                            ),
                          ),
                          TextButton(onPressed: () => Navigator.pop(context), child: Text('Ok')),
                        ],
                      ),
                      onClosing: () {},
                    );
                  });
              //setState(() {});
              return value;;
            },
            decoration: InputDecoration(
              labelText: labelText,
              prefixIcon: Icon(Icons.calendar_today),
              border: OutlineInputBorder(),
            ),
          );
        }
    );
  }
1988
  • 309
  • 2
  • 15
  • 1
    You're using an old version of the package which doesn't have Null Safety. So, update it to the latest version as mentioned on its page - [DateTime Picker ormat](https://pub.dev/packages/datetime_picker_formfield). And ignore the answers which say run the project without Null Safety, that's not a solution but a hack. Also, the package is very basic, you could've implemented these things without even using the package. – Lalit Fauzdar Sep 08 '22 at 10:47
  • thank you Lalit, your answer is helpful! I never expected it was from outdated dependency – 1988 Sep 12 '22 at 04:34

3 Answers3

0

In Android Studio:

Run → Edit Configurations → Add Additional Run args → --no-sound-null-safety

enter image description here

MohitJadav86
  • 782
  • 3
  • 11
0

datetime_picker_formfield has null safe enabled. It seems you have to update the datetime_picker_formfield package version in pubspec.yaml.

The latest nulls datetime_picker_formfield version is 2.0.1

Example

dependencies:
    datetime_picker_formfield: ^2.0.1
Sujan Gainju
  • 4,273
  • 2
  • 14
  • 34
0

seems like your package needs to be updated please make sure when you run your code your package should be updated

datetime_picker_formfield: ^2.0.1

use this package in your pubspec yaml if you face any issue in your code and you want to use your old code please hit this command

flutter run --no-sound-null-safety

MuhammadOmar
  • 518
  • 1
  • 4
  • 14