在浏览“为Flutter应用程序添加交互性”的教程时,我不断地看到这个错误消息:
类型“IconData”不是类型“Widget”的子类型
由于我缺乏经验,我不知道补救措施是什么。由于这是一个教程,我找到了答案代码,并将其复制粘贴到Visual Studio中,只是为了看看它应该是什么样子。我收到了同样的错误。我通过stack overflow和github做了一些检查。我找到的示例在故障代码中特别调用了IconData。在教程代码中,它只调用一次IconData,这是一个完全不同的小部件。
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
Widget titleSection = Container(
padding: const EdgeInsets.all(32),
child: Row(
children: [
Expanded(
/*1*/
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
/*2*/
Container(
padding: const EdgeInsets.only(bottom: 8),
child: Text(
'Oeschinen Lake Campground',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
),
Text(
'Kandersteg, Switzerland',
style: TextStyle(
color: Colors.grey[500],
),
),
],
),
),
FavoriteWidget(),
],
),
);
Color color = Theme.of(context).primaryColor;
Widget buttonSection = Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
_buildButtonColumn(color, Icons.call, 'CALL'),
_buildButtonColumn(color, Icons.near_me, 'ROUTE'),
_buildButtonColumn(color, Icons.share, 'SHARE'),
],
),
);
Widget textSection = Container(
padding: const EdgeInsets.all(32),
child: Text(
'Lake Oeschinen lies at the foot of the Blüemlisalp in the Bernese '
'Alps. Situated 1,578 meters above sea level, it is one of the '
'larger Alpine Lakes. A gondola ride from Kandersteg, followed by a '
'half-hour walk through pastures and pine forest, leads you to the '
'lake, which warms to 20 degrees Celsius in the summer. Activities '
'enjoyed here include rowing, and riding the summer toboggan run.',
softWrap: true,
),
);
return MaterialApp(
title: 'Flutter layout demo',
home: Scaffold(
appBar: AppBar(
title: Text('Flutter layout demo'),
),
body: ListView(
children: [
Image.asset(
'images/lake.jpg',
width: 600,
height: 240,
fit: BoxFit.cover,
),
titleSection,
buttonSection,
textSection,
],
),
),
);
}
Column _buildButtonColumn(Color color, IconData icon, String label) {
return Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(icon, color: color),
Container(
margin: const EdgeInsets.only(top: 8),
child: Text(
label,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w400,
color: color,
),
),
),
],
);
}
}
// #docregion FavoriteWidget
class FavoriteWidget extends StatefulWidget {
@override
_FavoriteWidgetState createState() => _FavoriteWidgetState();
}
// #enddocregion FavoriteWidget
// #docregion _FavoriteWidgetState, _FavoriteWidgetState-fields, _FavoriteWidgetState-build
class _FavoriteWidgetState extends State<FavoriteWidget> {
// #enddocregion _FavoriteWidgetState-build
bool _isFavorited = true;
int _favoriteCount = 41;
// #enddocregion _FavoriteWidgetState-fields
// #docregion _toggleFavorite
void _toggleFavorite() {
setState(() {
if (_isFavorited) {
_favoriteCount -= 1;
_isFavorited = false;
} else {
_favoriteCount += 1;
_isFavorited = true;
}
});
}
// #enddocregion _toggleFavorite
// #docregion _FavoriteWidgetState-build
@override
Widget build(BuildContext context) {
return Row(
mainAxisSize: MainAxisSize.min,
children: [
Container(
padding: EdgeInsets.all(0),
child: IconButton(
padding: EdgeInsets.all(0),
alignment: Alignment.centerRight,
icon: (_isFavorited ? Icon(Icons.star) : Icon(Icons.star_border)),
color: Colors.red[500],
onPressed: _toggleFavorite,
),
),
SizedBox(
width: 18,
child: Container(
child: Text('$_favoriteCount'),
),
),
],
);
}
// #docregion _FavoriteWidgetState-fields
}错误还特别引用了第34 : 11行,即FavoriteWidget()行。
谢谢您抽时间见我
发布于 2020-11-19 00:10:29
你发布的代码对我来说很好。

只需确保添加正确的图像资源(images/lake.jpg),例如:

确保在pubspec.yaml中设置uses-material-design: true,以便将图标包含在应用程序中。
并尝试更新flutter并运行flutter doctor,看看是否有任何其他问题
https://stackoverflow.com/questions/64895560
复制相似问题