【问题标题】:Error:-The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type错误:-主体可能正常完成,导致返回“null”,但返回类型可能是不可为空的类型
【发布时间】:2021-11-15 12:24:47
【问题描述】:

我目前正在构建一个将货币列表返回到下拉菜单的 botcoin 代码应用程序。 我收到以下错误:-

错误:主体可能正常完成,导致返回“null”,但返回类型可能是不可为空的类型。 (body_might_complete_normally at [bitcoin_ticker] lib\price_screen.dart:10)

错误:不能将参数类型“List”分配给参数类型“List?”。 (argument_type_not_assignable 在 [bitcoin_ticker] lib\price_screen.dart:60)

这是我的代码

import 'package:flutter/material.dart';
import 'coin_data.dart';
int i = 0;
class PriceScreen extends StatefulWidget {
  @override
  _PriceScreenState createState() => _PriceScreenState();
}


List<DropdownMenuItem> getDropDownItems() {

  List<DropdownMenuItem<String>> dropdownItem = [];
  for (String currency in currenciesList) {
     DropdownMenuItem(
      child: Text(currency), //
      value: currency,
    );
  }
}
class _PriceScreenState extends State<PriceScreen> {
  late String selectedCurrency = 'USD';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('???? Coin Ticker'),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          Padding(
            padding: EdgeInsets.fromLTRB(18.0, 18.0, 18.0, 0),
            child: Card(
              color: Colors.lightBlueAccent,
              elevation: 5.0,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10.0),
              ),
              child: Padding(
                padding: EdgeInsets.symmetric(vertical: 15.0, horizontal: 28.0),
                child: Text(
                  '1 BTC = ? USD',
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: 20.0,
                    color: Colors.white,
                  ),
                ),
              ),
            ),
          ),
          Container(
            height: 150.0,
            alignment: Alignment.center,
            padding: EdgeInsets.only(bottom: 30.0),
            color: Colors.lightBlue,
            child: DropdownButton<String>(
              value:selectedCurrency,//Default value
              items: getDropDownItems(),
             onChanged:(value){//it is like an on Pressed Button
                setState(() {
                  selectedCurrency = value!;
                  print(selectedCurrency);
                });
             },
            ),
          ),
        ],
      ),
    );
  }

}

【问题讨论】:

  • 您的getDropDownItems 函数缺少return 语句,因此不会返回任何内容。此外,它的for-loop 主体不会对其构造的DropdownMenuItem 对象做任何事情。
  • @jamesdlin 我编写了 for 循环来访问列表并将其打印在下拉列表中..你能告诉我如何更改 for 循环

标签: android flutter android-studio dart non-nullable


【解决方案1】:

显示警告是因为 getDropDownItems 没有从函数返回任何内容。此外,您没有更新 for 循环中的列表。可以参考以下代码

  List<DropdownMenuItem> getDropDownItems() {
    List<DropdownMenuItem> dropdownItem = currenciesList
        .map((currency) => DropdownMenuItem(
              child: Text(currency), //
              value: currency,
            ))
        .toList();

    return dropdownItem;
  }

【讨论】:

    猜你喜欢
    • 2021-09-29
    • 2021-06-26
    • 2022-08-06
    • 2021-11-21
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多