Home > Back-end >  dart constructor return null
dart constructor return null

Time:10-05

This is MainPage class I pass parameters from one class to MainPage class. Before passing the argument I print the list. List values are not null. But after pass the parameter to MainPage returns 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),
            )),
          ),
        ))
  ],
);
 }
}

This is my full code.

I have two class mainpage and cards. Both are passing arguments with each other.

CodePudding user response:

try to replace the userPickedList!.last with userPickedList?.last

CodePudding user response:

If you make your MainPage visible (for example give it a pink background) you will see that your problem is not with your parameter being null, your problem is that it is never displayed at all.

It's not in the tree of the build method.

It is hard to know what you wanted to achieve. You need to put your MainPage into the tree of visible elements you return from your build function. Your Cards class has the same problems. It's nto in the tree you return from your build method. It will never be visible.

In addition, I encourage you to read a tutorial for null-safety, you are making your life a lot harder than it should be. Once you understand it, there will be a lot less guessing and a lot less ! to place around your code. The better you get at it, the more helpful your compiler messages will become.

  • Related