Reputation: 1
I'm getting an error that i'm not understanding in my flutter code.
this is my code:
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])],
);
}
}
this is the full error message :
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])],
Can you help me figure out the problem ?
Upvotes: -1
Views: 3359
Reputation: 182
For the error
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])],
You have an error in this line :
AssetImage('assets/'+this.jour+"/"+this.heure+"/"+this.dirs[temp])
Error: this.dirs[temp]
You can't access to the List<String>
data giving it a String. You need to give it a Integer like an array. Like this.dirs[4]
But because you're doing (var temp in dirs)
you can simply give the temp
value. (Because is the same result than this.dirs[index]
)
So it should be like this:
AssetImage('assets/'+this.jour+"/"+this.heure+"/"+temp);
For the error
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])],
I think you need to wrap the AssetImage in a container
Upvotes: 1
Reputation: 587
you're missing a semicolon after the constructor
ReusableScreen({
@required this.jour,
@required this.heure,
});
Upvotes: 7