【问题标题】:The type 'DropDownField' is declared with 0 type parameters, but 1 type arguments were given使用 0 个类型参数声明类型 \'DropDownField\',但给出了 1 个类型参数
【发布时间】:2022-08-12 21:10:25
【问题描述】:
Widget locationList(String airportId) {
    return Container(
      child: StreamBuilder<QuerySnapshot>(
        stream: FirebaseFirestore.instance.collection(\"Airports\").doc(widget.airportId).collection(\"Locations\").snapshots(),
        builder: (context, snapshot) {
          if(snapshot.data == null){
           return Container();
          } else {
            List<DropdownMenuItem<String>> locationItem = [];
            for(int i=0;i<snapshot.data!.docs.length;i++){
              DocumentSnapshot data = snapshot.data!.docs[i];
              locationItem.add(
                DropdownMenuItem<String>(
                  child: Text(
                    data[\"Location Name\"],
                  ),
                  value: \"${data[\"Location Name\"]}\",
                )
              );
            }
            return Form(
              key: _formKey,
              child: Container(
                alignment: Alignment.center,
                height: 55,
                width: 200,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(10),
                  border: Border.all(color: Colors.black,width: 2)
                ),
                child: DropdownButtonHideUnderline(
                  child: DropDownField<DropdownMenuItem<String>>(
                    value: value,
                    required: true,
                    items: locationItem,
                    enabled: true,
                    strict: false,
                    itemsVisibleInDropdown: 5,
                    onValueChanged: (value) {
                      setState(() {
                        this.value = value!;
                        locationId = value;
                       ** print(value);**
                        print(locationId);
                      });
                    },
                  ),
                ),
              ),
            );
          }
        },
      ),
    );
  }
  • DropDownField 是来自包还是您创建的小部件?如果它是一个包,你可以分享你正在使用的包的链接吗?否则,您可以分享您的DropDownField 的代码吗?

标签: flutter dart dropdown


【解决方案1】:

您应该使用“DropdownButton”或“DropdownButtonFormField”而不是“DropDownField”。

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: const Center(
          child: MyStatefulWidget(),
        ),
      ),
    );
  }
}

class MyStatefulWidget extends StatefulWidget {
  const MyStatefulWidget({Key? key}) : super(key: key);

  @override
  State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  String dropdownValue = 'One';

  @override
  Widget build(BuildContext context) {
    return DropdownButtonHideUnderline(
      child:DropdownButton<String>(
      value: dropdownValue,
      icon: const Icon(Icons.arrow_downward),
      elevation: 16,
      style: const TextStyle(color: Colors.deepPurple),
      underline: Container(
        height: 2,
        color: Colors.deepPurpleAccent,
      ),
      onChanged: (String? newValue) {
        setState(() {
          dropdownValue = newValue!;
        });
      },
      items: <String>['One', 'Two', 'Free', 'Four']
          .map<DropdownMenuItem<String>>((String value) {
        return DropdownMenuItem<String>(
          value: value,
          child: Text(value),
        );
      }).toList(),
    ),);
  }
}

【讨论】:

    【解决方案2】:

    这个问题很老,但可能像我这样的新手可以从这个答案中获得帮助。 就我而言,我将小部件声明为

    class CandleStickChartWidget extends StatefulWidget<CandleStikState>
    

    代替

    class CandleStickChartWidget extends StatefulWidget
    

    更正到第二行修复了它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-17
      • 2020-04-12
      • 1970-01-01
      • 2018-09-28
      • 2021-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多