【发布时间】: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 不应该得到一个空参数,为什么不让参数非空呢?然后你的编译器可以告诉你问题出在哪里。
-
我不知道哪里出错了。我将一个字符串传递给主页,但我使用一个文本小部件来显示该字符串。