【发布时间】:2021-08-04 17:52:51
【问题描述】:
尝试将图标添加到我的列表磁贴后,我经常收到此错误:
════════ Exception caught by widgets library ═══════════════════════════════════
The following _TypeError was thrown building Alertbox(dirty, state: _AlertboxState#08465):
type 'Null' is not a subtype of type 'Icon'
The relevant error-causing widget was
Alertbox
lib/screens/team_screen.dart:58
When the exception was thrown, this was the stack
#0 _AlertboxState.build
package:trainings_app/widgets/alertbox_widget.dart:76
#1 StatefulElement.build
package:flutter/…/widgets/framework.dart:4691
#2 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4574
#3 StatefulElement.performRebuild
package:flutter/…/widgets/framework.dart:4746
#4 Element.rebuild
package:flutter/…/widgets/framework.dart:4267
...
════════════════════════════════════════════════════════════════════════════════
以下是错误引用的类: team_screen
void newTeam() {
showDialog<Alertbox>(
context: context,
builder: (BuildContext context) {
return Alertbox('Namen auswählen:', addTeam);
},
);
}
alertbox_widget:错误是由 TextField 中的“onSubmitted”引起的
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:trainings_app/config/palette.dart';
class Alertbox extends StatefulWidget {
final String title;
final Function(String, Icon) parseText;
const Alertbox(this.title, this.parseText);
@override
_AlertboxState createState() => _AlertboxState(title, parseText);
}
class _AlertboxState extends State<Alertbox> {
final String title;
final Function(String, Icon) parseText;
final textController = TextEditingController();
_AlertboxState(this.title, this.parseText);
var iconChoice;
List icons = [
Icons.sports_volleyball_outlined,
Icons.sports_handball_outlined,
Icons.sports_baseball_outlined,
];
@override
void dispose() {
textController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Dialog(
backgroundColor: Colors.transparent,
elevation: 0,
insetPadding: EdgeInsets.all(10),
child: Center(
child: Container(
decoration: new BoxDecoration(
borderRadius: new BorderRadius.all(const Radius.circular(20)),
color: Colors.white,
),
padding: const EdgeInsets.all(16.0),
child: SizedBox(
height: 100,
child: Column(
children: [
Text(
title,
style: TextStyle(
fontSize: 18,
),
),
Row(
children: [
DropdownButton(
value: iconChoice,
onChanged: (newIcon) {
setState(() {
iconChoice = newIcon;
});
},
items: icons.map((icon) {
return DropdownMenuItem(
value: icon,
child: Icon(icon),
);
}).toList(),
),
SizedBox(width: 12),
Expanded(
child: TextField(
onSubmitted: parseText(textController.text, iconChoice),
autofocus: true,
textAlign: TextAlign.center,
controller: textController,
),
),
SizedBox(width: 12),
ElevatedButton(
onPressed: () {
parseText(textController.text, iconChoice);
},
child: const Icon(CupertinoIcons.checkmark),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Palette.orange),
),
),
SizedBox(width: 8),
],
),
],
),
),
),
),
);
}
}
我希望任何人都可以帮助我或有某种解决方案/解决方法来帮助解决这个问题
【问题讨论】: