Flutter DropDownButttonFormField `方法“[]”在 null 上调用。接收者:空`

问题描述 投票:0回答:1

我正在尝试创建一个表单页面,它包含多个小部件,例如 FreeTextForm 和 DropdownButtonFormField。下拉数据是使用

sqflite
从 sqlite 获取的。 如果我在与文本表单交互后与下拉菜单交互,则会发生此问题,但如果我先与下拉菜单交互,然后转到文本表单,则该问题不会出现(如果在与文本表单交互后我与下拉交互,则会出现该问题)下)。

这是表单页面的代码

class ActivityFormPage extends StatelessWidget {
  const ActivityFormPage({super.key});

  @override
  Widget build(BuildContext context) {
    final ActivityController c = Get.put(
      ActivityController(
        id: Get.arguments["id"],
        db: Get.find<DBInstance>().database,
      ),
    );

    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          onPressed: () => Get.back(),
          icon: const Icon(
            Icons.arrow_back,
          ),
        ),
        title: Text(
          "New Activity",
          textAlign: TextAlign.left,
          style: context.textTheme.titleLarge,
        ),
      ),
      body: SafeArea(
        child: SingleChildScrollView(
          child: Padding(
            padding: EdgeInsets.all(UIConst.standardPadding * 2),
            child: Column(
              children: [
                Form(
                  key: c.newActivityKey,
                  child: Column(
                    children: [
                      CustomFreeTextForm(
                        label: "Title",
                        hint: "Write activity title",
                        state: c.state.title,
                        maxLines: 1,
                      ),
                      SizedBox(
                        height: UIConst.standardGapHeight * 2,
                      ),
                      CustomDropDownForm<CategoryModel>(
                        label: "Category",
                        hint: "Select category",
                        noDataHint: "No category available",
                        itemArray: c.getAllCategories(),
                        state: c.state.categoryId,
                        isDisabled: false,
                      ),
                      SizedBox(
                        height: UIConst.standardGapHeight * 2,
                      ),
                      Obx(() {
                        return CustomDropDownForm<SubCategoryModel>(
                          label: "Sub Category",
                          hint: "Select sub category",
                          noDataHint: "No sub category available",
                          itemArray: c.getAllSubCategories(
                            c.state.categoryId.toInt(),
                          ),
                          state: c.state.subCategoryId,
                          isDisabled: c.state.categoryId.toInt() == 0,
                        );
                      }),
                      SizedBox(
                        height: UIConst.standardGapHeight * 2,
                      ),
                      Row(
                        children: [
                          CustomTimePicker(
                            width: MediaQuery.of(context).size.width * 0.4,
                            label: "Start time",
                            hint: "Select start time",
                            state: c.state.startTime,
                            onChange: c.formatDisplayTime,
                          ),
                          const Expanded(
                            flex: 1,
                            child: SizedBox(),
                          ),
                          CustomTimePicker(
                            width: MediaQuery.of(context).size.width * 0.4,
                            label: "End time",
                            hint: "Select end time",
                            state: c.state.endTime,
                            onChange: c.formatDisplayTime,
                          ),
                        ],
                      ),
                      SizedBox(
                        height: UIConst.standardGapHeight * 2,
                      ),
                      CustomFreeTextForm(
                        label: "Description",
                        hint: "Write description",
                        state: c.state.description,
                        maxLines: 5,
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

这是 CustomDropDownForm 的代码

class CustomDropDownForm<T extends BaseModel> extends StatelessWidget {
  final String label;
  final String hint;
  final String noDataHint;
  final Future<List<T>> itemArray;
  final RxInt state;
  final bool isDisabled;

  const CustomDropDownForm({
    super.key,
    required this.label,
    required this.hint,
    required this.noDataHint,
    required this.itemArray,
    required this.state,
    required this.isDisabled,
  });

  @override
  Widget build(BuildContext context) {
    return AbsorbPointer(
      absorbing: isDisabled,
      child: Opacity(
        opacity: isDisabled ? 0.5 : 1.0,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              label,
              style: context.textTheme.bodySmall,
            ),
            SizedBox(
              height: UIConst.standardGapHeight,
            ),
            FutureBuilder<List<T>>(
              future: itemArray,
              builder: (context, snapshot) {
                if (snapshot.hasData && snapshot.data!.isNotEmpty) {
                  return DropdownButtonFormField<int>(
                    value: null,
                    items: snapshot.data!.map((e) {
                      return DropdownMenuItem(
                        value: e.id,
                        child: Text(
                          e.name,
                          style: context.textTheme.bodyMedium,
                        ),
                      );
                    }).toList(),
                    onChanged: isDisabled
                        ? null
                        : (value) {
                            state.value = value!;
                          },
                    hint: Text(
                      hint,
                      style: context.textTheme.bodyMedium!.copyWith(
                        color: state.value == 0 ? Colors.black : Colors.grey,
                      ),
                    ),
                    decoration: dropDownButtonDecoration,
                    elevation: 0,
                  );
                } else {
                  return DropdownButtonFormField(
                    value: null,
                    items: const [],
                    onChanged: null,
                    hint: Text(noDataHint),
                    decoration: dropDownButtonDecoration,
                  );
                }
              },
            ),
          ],
        ),
      ),
    );
  }
}

这是错误的样子

enter image description here

期望当我与其他小部件交互后与 CustomDropDownForm 交互时,不会出现问题并且显示下拉项

flutter dart
1个回答
0
投票

Get.arguments 不为空,并且它包含您尝试访问的密钥。您可以通过在访问该值之前执行空检查来完成此操作。

final id = Get.arguments?["id"];
final ActivityController c = Get.put(
  ActivityController(
    id: id,
    db: Get.find<DBInstance>().database,
  ),
);
© www.soinside.com 2019 - 2024. All rights reserved.