can't have a value of 'null'的各种错误

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

我正在阅读 Simone Alessandria 编写的《Flutter Cookbook - 第二版》,并在

star.dart
文件中的第 4 章中出现错误:

Launching lib/main.dart on Chrome in debug mode...
lib/star.dart:8:9: Error: The parameter 'key' can't have a value of 'null' because of its type 'Key', but the implicit default value is 'null'.
 - 'Key' is from 'package:flutter/src/foundation/key.dart' ('../../../../SDK/flutter/packages/flutter/lib/src/foundation/key.dart').
Try adding either an explicit non-'null' default value or the 'required' modifier.
    Key key,
        ^^^
lib/star.dart:9:10: Error: The parameter 'color' can't have a value of 'null' because of its type 'Color', but the implicit default value is 'null'.
 - 'Color' is from 'dart:ui'.
Try adding either an explicit non-'null' default value or the 'required' modifier.
    this.color,
         ^^^^^
lib/star.dart:10:10: Error: The parameter 'size' can't have a value of 'null' because of its type 'double', but the implicit default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.
    this.size,
         ^^^^
lib/star.dart:65:9: Error: The parameter 'key' can't have a value of 'null' because of its type 'Key', but the implicit default value is 'null'.
 - 'Key' is from 'package:flutter/src/foundation/key.dart' ('../../../../SDK/flutter/packages/flutter/lib/src/foundation/key.dart').
Try adding either an explicit non-'null' default value or the 'required' modifier.
    Key key,
        ^^^
lib/star.dart:66:20: Error: The parameter 'value' can't have a value of 'null' because of its type 'int', but the implicit default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.
    @required this.value,
                   ^^^^^
Failed to compile application.


Exited (1).

这是

star.dart
文件:

import 'package:flutter/material.dart';

class Star extends StatelessWidget {
  final Color color;
  final double size;

  const Star({
    Key key,
    this.color,
    this.size,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: size,
      height: size,
      child: CustomPaint(
        painter: _StarPainter(color),
      ),
    );
  }
}

class _StarPainter extends CustomPainter {
  final Color color;

  _StarPainter(this.color);

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()..color = color;

    final path = Path();

    path.moveTo(size.width * 0.5, 0);
    path.lineTo(size.width * 0.618, size.height * 0.382);
    path.lineTo(size.width, size.height * 0.382);
    path.lineTo(size.width * 0.691, size.height * 0.618);
    path.lineTo(size.width * 0.809, size.height);
    path.lineTo(size.width * 0.5, size.height * 0.7639);
    path.lineTo(size.width * 0.191, size.height);
    path.lineTo(size.width * 0.309, size.height * 0.618);
    path.lineTo(size.width * 0.309, size.height * 0.618);
    path.lineTo(0, size.height * 0.382);
    path.lineTo(size.width * 0.382, size.height * 0.382);

    path.close();

    canvas.drawPath(path, paint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return false;
  }
}

class StarRating extends StatelessWidget {
  final Color color;
  final int value;
  final double starSize;

  const StarRating({
    Key key,
    @required this.value,
    this.color = Colors.deepOrange,
    this.starSize = 25,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Row(
      children: List.generate(
        value,
        (_) => Padding(
          padding: const EdgeInsets.all(2.0),
          child: Star(
            color: color,
            size: starSize,
          ),
        ),
      ),
    );
  }
}

完整代码可以在作者的GitHub

找到
flutter dart
1个回答
0
投票

改变

const StarRating({
    Key key,
    @required this.value,
    this.color = Colors.deepOrange,
    this.starSize = 25,
  }) : super(key: key);

  const StarRating({
    Key? key,
    @required this.value,
    this.color = Colors.deepOrange,
    this.starSize = 25,
  }) : super(key: key);

密钥应该是可选的

© www.soinside.com 2019 - 2024. All rights reserved.