gpt4 book ai didi

flutter - 如何解决错误: '_InternalLinkedHashMap>'类型不是 'List'类型的子类型

转载 作者:行者123 更新时间:2023-12-03 04:57:14 25 4
gpt4 key购买 nike

背景

我正在开发一个可帮助小学老师的应用程序。
该应用程序允许用户从字母表中选择字母,当他们继续进入下一个屏幕时,他们选择的字母应显示为可翻转卡片。一侧有字母,另一侧有图像。
这些卡应占据整个屏幕,并且用户可以滑动到下一张卡。

问题

当用户选择多个字母时,它们将被输入到列表中。当他们点击按钮以移动到下一个屏幕(带有可翻转卡)时,会收到以下错误消息:

“类型'_InternalLinkedHashMap>'不是类型'列表'的子类型”

代码

以下是字母选择屏幕的代码:

import 'package:flutter/material.dart';
import 'package:project_teacher/pages/allcards.dart';

class ChooseLetters extends StatefulWidget {
@override
_ChooseLettersState createState() => _ChooseLettersState();
}

class _ChooseLettersState extends State<ChooseLetters> {

List<String> alphabets = ['a','b','c','d','e','f',
'g','h','i','j','k','l','m','n','o','p',
'q','r','s','t','u','v','w','x','y','z'],

selection = [];

List alphabetCards = [AlphabetCard(),BAlphabetCard(),CAlphabetCard(),DAlphabetCard(),EAlphabetCard(),FAlphabetCard(),
GAlphabetCard(),HAlphabetCard(),IAlphabetCard(),JAlphabetCard(),KAlphabetCard(),LAlphabetCard(),MAlphabetCard(),NAlphabetCard(),OAlphabetCard(),PAlphabetCard(),
QAlphabetCard(),RAlphabetCard(),SAlphabetCard(),TAlphabetCard(),UAlphabetCard(),VAlphabetCard(),WAlphabetCard(),XAlphabetCard(),YAlphabetCard(),ZAlphabetCard()],

selectionCards = [];

@override
void initState(){
super.initState();
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.bottomLeft,
end: Alignment.topRight,
colors: [Colors.blueGrey[300], Colors.blueGrey])
),
padding:EdgeInsets.all(15),
alignment: Alignment.center,
child:GridView.builder(
gridDelegate:SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount:4,
mainAxisSpacing: 10, crossAxisSpacing: 10,
),
itemCount:alphabetCards.length,
itemBuilder:(con,ind){
return InkWell(
onTap:(){
setState((){
if(selectionCards.contains(alphabetCards[ind]))
selectionCards.remove(alphabetCards[ind]);
else
selectionCards.add(alphabetCards[ind]);
});
print(selectionCards);
},
child: Container(
padding:EdgeInsets.all(10),
alignment:Alignment.center,
decoration:BoxDecoration(
color:selectionCards.contains(alphabetCards[ind]) ? Colors.orange : Colors.white,
borderRadius:BorderRadius.circular(0)
),
child:Text(alphabets[ind],
style: TextStyle(
color:Colors.black,
fontSize: 50
)
)
),
);
},
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.pushNamed(context, '/tabbedcards', arguments: {
'selectionOfCards': selectionCards
});
},
backgroundColor: Colors.red,
),
);
}

}

这是可翻转式卡片屏幕的代码:
import 'package:flutter/material.dart';

class TabbedCards extends StatefulWidget {

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

class _TabbedCardsState extends State<TabbedCards> {

List selectionOfCards;

@override
Widget build(BuildContext context) {

selectionOfCards = ModalRoute.of(context).settings.arguments;

return MaterialApp(
home: DefaultTabController(
length: selectionOfCards.length,
child: Scaffold(
body: TabBarView(
children: selectionOfCards
),
),
),
);
}
}

感谢您的阅读和提供的任何帮助。

最佳答案

您只需进行一些更改即可使其工作。

List<String> alphabets = ['a','b','c','d','e','f',
'g','h','i','j','k','l','m','n','o','p',
'q','r','s','t','u','v','w','x','y','z'],

selection = [];

在这里,您尝试创建字符串列表并添加selection = []?
List<String> alphabets = ['a','b','c','d','e','f',
'g','h','i','j','k','l','m','n','o','p',
'q','r','s','t','u','v','w','x','y','z'];

现在将类型提供给小部件列表。
  List<Widget> alphabetCards = [
AlphabetCard(),
BAlphabetCard(),
CAlphabetCard(),
DAlphabetCard(),
EAlphabetCard(),
....
];
List<Widget> selectionCards = <Widget>[];

现在,如下更改您的标签 View 。
class _TabbedCardsState extends State<TabbedCards> {
Map<String, List> selectionOfCards;
List<Widget> data;

@override
Widget build(BuildContext context) {
selectionOfCards = ModalRoute.of(context).settings.arguments;
data = selectionOfCards['selectionOfCards'];
print(data.toString());
return MaterialApp(
home: DefaultTabController(
length: data.length,
child: Scaffold(
body: TabBarView(
children: data,
),
),
),
);
}
}

完整的演示代码:
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: '/',
routes: {'/tabbedcards': (context) => TabbedCards()},
home: ChooseLetters(),
);
}
}

class ChooseLetters extends StatefulWidget {
@override
_ChooseLettersState createState() => _ChooseLettersState();
}

class _ChooseLettersState extends State<ChooseLetters> {
List<String> alphabets = [
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z'
];

List<Widget> alphabetCards = [
AlphabetCard(),
BAlphabetCard(),
CAlphabetCard(),
DAlphabetCard(),
EAlphabetCard(),
];
List<Widget> selectionCards = <Widget>[];

@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.bottomLeft,
end: Alignment.topRight,
colors: [Colors.blueGrey[300], Colors.blueGrey])),
padding: EdgeInsets.all(15),
alignment: Alignment.center,
child: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4,
mainAxisSpacing: 10,
crossAxisSpacing: 10,
),
itemCount: alphabetCards.length,
itemBuilder: (con, ind) {
return InkWell(
onTap: () {
setState(() {
print("print ${alphabetCards[ind].toString()}");
if (selectionCards.contains(alphabetCards[ind]))
selectionCards.remove(alphabetCards[ind]);
else
selectionCards.add(alphabetCards[ind]);
});
print(selectionCards);
},
child: Container(
padding: EdgeInsets.all(10),
alignment: Alignment.center,
decoration: BoxDecoration(
color: selectionCards.contains(alphabetCards[ind])
? Colors.orange
: Colors.white,
borderRadius: BorderRadius.circular(0)),
child: Text(alphabets[ind],
style: TextStyle(color: Colors.black, fontSize: 50))),
);
},
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.pushNamed(context, '/tabbedcards',
arguments: {'selectionOfCards': selectionCards});
},
backgroundColor: Colors.red,
),
);
}
}

class AlphabetCard extends StatelessWidget {
const AlphabetCard({Key key}) : super(key: key);

@override
Widget build(BuildContext context) {
return Container(
child: Text(" A hello"),
);
}
}

class BAlphabetCard extends StatelessWidget {
const BAlphabetCard({Key key}) : super(key: key);

@override
Widget build(BuildContext context) {
return Container(
child: Text(" B hello"),
);
}
}

class CAlphabetCard extends StatelessWidget {
const CAlphabetCard({Key key}) : super(key: key);

@override
Widget build(BuildContext context) {
return Container(
child: Text(" C hello"),
);
}
}

class DAlphabetCard extends StatelessWidget {
const DAlphabetCard({Key key}) : super(key: key);

@override
Widget build(BuildContext context) {
return Container(
child: Text(" D hello"),
);
}
}

class EAlphabetCard extends StatelessWidget {
const EAlphabetCard({Key key}) : super(key: key);

@override
Widget build(BuildContext context) {
return Container(
child: Text(" E hello"),
);
}
}

class TabbedCards extends StatefulWidget {
@override
_TabbedCardsState createState() => _TabbedCardsState();
}

class _TabbedCardsState extends State<TabbedCards> {
Map<String, List> selectionOfCards;
List<Widget> data;

@override
Widget build(BuildContext context) {
selectionOfCards = ModalRoute.of(context).settings.arguments;
data = selectionOfCards['selectionOfCards'];
print(data.toString());
return MaterialApp(
home: DefaultTabController(
length: data.length,
child: Scaffold(
body: TabBarView(
children: data,
),
),
),
);
}
}

关于flutter - 如何解决错误: '_InternalLinkedHashMap<String, List<dynamic>>'类型不是 'List<dynamic>'类型的子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60362600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com