我在flutter代码中得到了一个我不理解的错误。
这是我的代码:
import 'package:flutter/material.dart';
class ReusableScreen extends StatelessWidget{
final List <String> dirs=['11', '12', '13', '14', '21', '22', '23', '24', '31', '32', '33', '34', '41', '42', '43', '44'];
final String jour;
final String heure;
ReusableScreen({
@required this.jour,
@required this.heure,
})
@override
Widget build(BuildContext context) {
return ListView(
scrollDirection: Axis.horizontal,
children: [for (var temp in dirs) AssetImage('assets/'+this.jour+"/"+this.heure+"/"+this.dirs[temp])],
);
}
}这是完整的错误消息:
lib/wid.dart:14:3: Error: Expected '{' before this.
@override
^
lib/wid.dart:18:101: Error: A value of type 'String' can't be assigned to a variable of type 'int'.
children: [for (var temp in dirs) AssetImage('assets/'+this.jour+"/"+this.heure+"/"+this.dirs[temp])],
^
lib/wid.dart:18:41: Error: A value of type 'AssetImage' can't be assigned to a variable of type 'Widget'.
- 'AssetImage' is from 'package:flutter/src/painting/image_resolution.dart' ('/C:/flutter/packages/flutter/lib/src/painting/image_resolution.dart').
- 'Widget' is from 'package:flutter/src/widgets/framework.dart' ('/C:/flutter/packages/flutter/lib/src/widgets/framework.dart').
children: [for (var temp in dirs) AssetImage('assets/'+this.jour+"/"+this.heure+"/"+this.dirs[temp])],你能帮我解决这个问题吗?
发布于 2021-02-07 02:11:47
构造函数后面缺少一个分号
ReusableScreen({
@required this.jour,
@required this.heure,
});发布于 2021-02-07 03:45:15
对于错误
lib/wid.dart:18:101: Error: A value of type 'String' can't be assigned to a variable of type 'int'.
children: [for (var temp in dirs) AssetImage('assets/'+this.jour+"/"+this.heure+"/"+this.dirs[temp])],您在这一行中有一个错误:
AssetImage('assets/'+this.jour+"/"+this.heure+"/"+this.dirs[temp])错误: this.dirstemp
您不能访问List<String>数据,只给它一个字符串。你需要给它一个像数组一样的整数。像this.dirs[4]一样
但是因为您正在执行(var temp in dirs),所以可以简单地给出temp的值。(因为结果与this.dirs[index]相同)
所以它应该是这样的:
AssetImage('assets/'+this.jour+"/"+this.heure+"/"+temp);
对于错误
Error: A value of type 'AssetImage' can't be assigned to a variable of type 'Widget'.
- 'AssetImage' is from 'package:flutter/src/painting/image_resolution.dart' ('/C:/flutter/packages/flutter/lib/src/painting/image_resolution.dart').
- 'Widget' is from 'package:flutter/src/widgets/framework.dart' ('/C:/flutter/packages/flutter/lib/src/widgets/framework.dart').
children: [for (var temp in dirs) AssetImage('assets/'+this.jour+"/"+this.heure+"/"+this.dirs[temp])],我认为您需要将AssetImage包装在一个容器中
https://stackoverflow.com/questions/66080166
复制相似问题