flutter 无法将参数类型“List”< dynamic>赋给参数类型“List< String>”,

im9ewurl  于 2022-11-17  发布在  Flutter
关注(0)|答案(2)|浏览(162)

目前,我正在做一个在以前版本上开发的Flutter教程。错误发生在我们更新到最新版本的Dart和Flutter之后,可能是由于零安全。

import 'package:bitcoin_ticker/coin_data.dart';
import 'package:flutter/material.dart';

class PriceScreen extends StatefulWidget {
  @override
  _PriceScreenState createState() => _PriceScreenState();
}

class _PriceScreenState extends State<PriceScreen> {
  
  String? selectedCurrency = 'USD';

  List<DropdownMenuItem> getDropdownItems(){
    List<DropdownMenuItem<String>> dropdownItems = [];

    for (String currency in currenciesList){
      var newItem = DropdownMenuItem(
        child: Text(currency), 
        value: currency,
      );
      dropdownItems.add(newItem);
    }

    return dropdownItems;
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('🤑 Coin Ticker'),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          
          Container(
            height: 150.0,
            alignment: Alignment.center,
            padding: EdgeInsets.only(bottom: 30.0),
            color: Colors.lightBlue,
            child: DropdownButton<String>(
              value: selectedCurrency,
              items: getDropdownItems(),    // This is where I'm getting an error
            onChanged: (value){
              setState(() {
                selectedCurrency = value;
              });;
            }),
          ),
        ],
      ),
    );
  }
}

我是一个初学者,以Flutter和飞镖,所以任何帮助是赞赏。

rta7y2nd

rta7y2nd1#

只需从DropdownButton<String>中删除<String>

child: DropdownButton(
              value: selectedCurrency,
              items: getDropdownItems(),    // This is where I'm getting an error
            onChanged: (value){
              setState(() {
                selectedCurrency = value;
              });;
            }),
v2g6jxz6

v2g6jxz62#

您应该更改此行

List<DropdownMenuItem> getDropdownItems(){

对此

List<DropdownMenuItem<String>> getDropdownItems(){

相关问题