【问题标题】:dart constructor return null飞镖构造函数返回null
【发布时间】:2021-10-05 22:22:58
【问题描述】:

这是 MainPage 类,我将参数从一个类传递给 MainPage 类。在传递参数之前,我打印列表。列表值不为空。但是将参数传递给MainPage后返回null。

import 'package:flutter/material.dart';
import 'package:rummy/gameLogic/mainpage.dart';

class Cards extends StatefulWidget {
  const Cards({Key? key, this.cardStatus}) : super(key: key);
  final bool? cardStatus;

  @override
  State<Cards> createState() => _CardsState();
   }

  class _CardsState extends State<Cards> {
 List<String>? userPickedList = [];
  List<String> list = [
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'10',

  ];
 @override
Widget build(BuildContext context) {
return Container(
    height: MediaQuery.of(context).size.height / 6,
    width: MediaQuery.of(context).size.width / 1.0001,
    color: Colors.transparent,
    child: ReorderableListView(
      scrollDirection: Axis.horizontal,
      onReorder: (int oldIndex, int newIndex) {
        setState(() {
          if (oldIndex < newIndex) {
            newIndex--;
          }
          final String item = list.removeAt(oldIndex);
          list.insert(newIndex, item);
        });
      },
      children: <Widget>[
        for (int index = 0; index < list.length; index++)
          GestureDetector(
            key: ValueKey(index),
            onTap: () {
              setState(() {
                userPickedList!.add(list[index]);

                widget.cardStatus! ? list.removeAt(index) : null;
                Mainpage(
                  //   tappedStatus: tapped,
                  userPick: userPickedList?.last,
                );
                print(userPickedList?.last);
              });
            },
            child: Card(
              child: Container(
                height: MediaQuery.of(context).size.height / 4,
                width: MediaQuery.of(context).size.width / 16.7,
                alignment: Alignment.bottomCenter,
                child: Text(list[index]),
              ),
            ),
          )
      ],
    ));
   }
  }


import 'package:rummy/gameLogic/cards.dart';

class Mainpage extends StatefulWidget {
const Mainpage(
  {Key? key, this.tappedStatus,this.userPick, 
 this.shuffledCard})
  : super(key: key);
final bool? tappedStatus;
final String? shuffledCard;
 final String? userPick;

@override
_MainpageState createState() => _MainpageState();
 }

class _MainpageState extends State<Mainpage> {
 @override
 void initState() {
   super.initState();



}

  bool cardDrop = false;

  @override
  Widget build(BuildContext context) {
  return Stack(
  children: [
    Positioned(
      bottom: 0,
      child: Container(
          color: Colors.white, child: Cards(cardStatus: cardDrop)),
    ),
    Positioned(
        top: MediaQuery.of(context).size.height / 3.5,
        left: MediaQuery.of(context).size.width / 2.8,
        child: Container(
          padding: const EdgeInsets.fromLTRB(5, 0, 0, 0),
          height: MediaQuery.of(context).size.height / 4,
          width: MediaQuery.of(context).size.height / 6,
          color: Colors.black,
          child: const Center(
            child: Text(
              'Shuffled card',
              style: TextStyle(color: Colors.white),
            ),
          ),
        )),
    Positioned(
        top: MediaQuery.of(context).size.height / 3.5,
        left: MediaQuery.of(context).size.width / 1.8,
        child: Container(
          padding: const EdgeInsets.fromLTRB(5, 0, 0, 0),
          height: MediaQuery.of(context).size.height / 4,
          width: MediaQuery.of(context).size.height / 6,
          color: Colors.black,
          child: Center(
            child: Text(
              widget.userPick.toString(),
              style: const TextStyle(color: Colors.white),
            ),
          ),
        )),
    Positioned(
        top: MediaQuery.of(context).size.height / 2,
        right: MediaQuery.of(context).size.width / 38,
        child: GestureDetector(
          onTap: () {
            setState(() {
              cardDrop = true;
              Cards(cardStatus: cardDrop);
              if (widget.tappedStatus == true) {
                cardDrop = false;
                Cards(cardStatus: cardDrop);
              }
              // Future.delayed(const Duration(seconds: 10));
            });
          },
          child: Container(
            height: MediaQuery.of(context).size.height / 6,
            width: MediaQuery.of(context).size.width / 8,
            color: Colors.black,
            child: const Center(
                child: Text(
              'Drop Card',
              style: TextStyle(color: Colors.white),
            )),
          ),
        ))
  ],
);
 }
}

这是我的完整代码。

我有两个班级主页和卡片。两者都在互相传递参数。

【问题讨论】:

  • 请发帖minimal reproducible example。请注意,如果您必须使用! 来覆盖在您自己的代码中 的空安全性,那么您就犯了一个错误。此功能是最后的解决方法,可以使用您无法更改的代码,而不是让您在自己的编码中变得懒惰。
  • 顺便说一句,如果 MainPage 不应该得到一个空参数,为什么不让参数非空呢?然后你的编译器可以告诉你问题出在哪里。
  • 我不知道哪里出错了。我将一个字符串传递给主页,但我使用一个文本小部件来显示该字符串。

标签: flutter dart


【解决方案1】:

尝试替换userPickedList!.last userPickedList?.last

【讨论】:

  • 我仍然是空的
【解决方案2】:

如果你让你的 MainPage 可见(例如给它一个粉红色的背景)你会发现你的问题不在于你的参数为空,你的问题是它根本不会显示。

它不在构建方法的树中。

很难知道您想要实现什么。您需要将 MainPage 放入从构建函数返回的可见元素树中。您的 Cards 课程也有同样的问题。它在您从构建方法返回的树中。它永远不可见。

此外,我鼓励你阅读零安全教程,你让你的生活变得比应有的更艰难。一旦你理解了它,你的代码周围就会少很多猜测和!。掌握得越好,编译器消息就越有用。

【讨论】:

  • 我打印了参数,但它返回 null。如果参数不包含任何数据,则它在文本中返回 null。如果出现任何值,则返回该值。但它只返回null。我改变了颜色,但什么也没发生。
  • 您的 MainPage 永远不会显示,因为它不在您返回的树中。问问自己,您期望 MainPage 会出现在哪里?为什么?您从未说过它应该在页面上的哪个位置,它的父级是什么。这不是您的参数的问题。
  • 好的,我明白什么是解决方案了。
猜你喜欢
  • 2022-07-09
  • 2018-10-20
  • 2018-10-29
  • 2020-04-03
  • 2019-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多