【问题标题】:Flutter 2.0 The argument type 'Color?' can't be assigned to the parameter type 'Color'Flutter 2.0 参数类型“颜色?”不能分配给参数类型“颜色”
【发布时间】:2021-06-05 13:29:16
【问题描述】:

在我将flutter sdk更新为>=2.12.0 The argument type 'Color?' can't be assigned to the parameter type 'Color',这是怎么回事?

Card(
  shape: RoundedRectangleBorder(
    side: BorderSide(color: Colors.blue[300], width: 2.0),
    borderRadius: BorderRadius.circular(15.0)
  ),
  child: Text('Demo')),

重现错误的完整代码:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: Card(
              shape: RoundedRectangleBorder(
                side: BorderSide(color: Colors.blue[300], width: 2.0),
                borderRadius: BorderRadius.circular(15.0),
              ),
              child: Text('Demo')),
        ),
      ),
    );
  }
}

【问题讨论】:

标签: flutter


【解决方案1】:

就这样做

 color: (Colors.blue[300])!,

这是 dart 中的一个功能 Null safty 欲了解更多信息,请查看此链接

https://medium.com/flutter/null-safety-flutter-tech-preview-cb5c98aba187

【讨论】:

  • 颜色警告消失了,现在我得到“无效的常量值”
  • 上面的链接没有解释感叹号,所以在这里阅读:stackoverflow.com/a/63253350/614026
  • 对这个功能很困惑,让颜色变得如此复杂,现在我无法将 Color.lightBlueAccent[700] 分配给原色 ...
  • Flutter 中的 null 安全性是该语言发生过的最糟糕的事情。让我处理空安全!现在解决这些愚蠢的问题需要很长时间!!!
【解决方案2】:

你也可以这样做

color: Colors.blue.shade300

给出相同的结果。

有关更多信息,请参阅颤振文档: https://api.flutter.dev/flutter/material/Colors-class.html

【讨论】:

  • 这对我有用,谢谢!
猜你喜欢
  • 2021-08-15
  • 2021-06-16
  • 2022-01-16
  • 2021-12-14
  • 2020-11-29
  • 2021-08-27
  • 2021-08-18
  • 2023-04-09
  • 2022-07-20
相关资源
最近更新 更多