Flutter dart generic error: type '(int?) => void' is not a subtype of type '(dynamic) => void' when use radio

Viewed 163

i want to make a stateful widget named "RadioGroup" like this

import 'package:flutter/material.dart';

class RadioGroupOptions<T> {
  String label;
  T value;

  RadioGroupOptions({
    required this.label,
    required this.value,
  });
}

class RadioGroup<T> extends StatefulWidget {
  List<RadioGroupOptions<T>> options = [];
  T? defaultValue;
  void Function(T? value) onChange;

  RadioGroup({
    Key? key,
    required this.options,
    required this.onChange,
    this.defaultValue,
  }) : super(key: key);

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

class _RadioGroupState<T> extends State<RadioGroup> {
  T? currentValue;

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
          children: widget.options
              .map((e) => Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Radio<T>(
                        value: e.value,
                        groupValue: currentValue,
                        onChanged: (T? value) => {
                          setState(() {
                            if (value == null) {
                              return;
                            }
                            currentValue = value;
                            widget.onChange(currentValue);
                          })
                        },
                      ),
                      Container(
                        margin: EdgeInsets.only(left: 10.0),
                        child: Text(e.label),
                      ),
                    ],
                  ))
              .toList()),
    );
  }

  @override
  void initState() {
    currentValue = widget.defaultValue;
    super.initState();
  }
}

it receive some options and a onChange callback, when radio value change, the callback function will notify outside

and then...

i make a dialog wrap the radioGroup like this

static Future<T?> showRadioGroupDialog<T>(
      String title, List<RadioGroupOptions<T>> radioOptions,
      { T? defaultValue, String msg = '', String actionText = '确定' }) async {
    T? selectResult = defaultValue;
    await Get.dialog(AlertDialog(
      title: Text(title),
      content: Column(
        children: [
          if (msg.isNotEmpty) Text(msg),
          RadioGroup<T>(
            options: radioOptions,
            defaultValue: defaultValue,
            onChange: (T? value) {
              selectResult = value;
            },
          ),
        ],
      ),
      actions: [
        TextButton(
          onPressed: () {
            Get.back();
          },
          child: Text(actionText),
        ),
      ],
    ));

    return selectResult;
  }

i will pass a generic into the dialog, but when i choice the option let the radioGroup emit the onChange callback i receive the error

type '(int?) => void' is not a subtype of type '(dynamic) => void'

the error raise on the widget.onChnage(currentValue)

how i can resolve this??

0 Answers
Related