1
votes

i'm new to flutter with dart programming. i found an error regarding my question, that is about "i created a model class and a list, model class has a member of type Color, then in the main.dart, i want to display a list of my model data in a ListView.builder, but when in a separate widget of container, everything else works well but color property is giving error, i tried to change the type of index parameter but error stays. "

here is code:

import 'package:flutter/material.dart';

 class ProductModel {
          
ProductModel(
this.title,
this.name,
this.image,
this.color
);

final String title;
final String name;
final String image;
final Color color;
}

final modelProduct = <ProductModel>[
 ProductModel(
"titile1",
"name1",
"https://image.freepik.com/free-vector/multitasking-concept-illustration-character_23-2148403716.jpg",
Colors.pink.withOpacity(0.4),
),
ProductModel(
"titile2",
"name2",
"https://image.freepik.com/free-vector/people-putting-puzzle-pieces-together_52683-28610.jpg",
Colors.blue.withOpacity(0.4),
),
ProductModel(
"titile3",
"name3",
"https://image.freepik.com/free-vector/people-using-online-apps-set_74855-4457.jpg",
Colors.yellow.withOpacity(0.4),
),
]

and the main.dart i skipped the boiler plate code of first flutter, and just copied that has the main thing of my concern,

class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: Text(widget.title),
  ),
  body: Container(
      child: ListView.builder(
          itemCount: modelProduct.length,
          itemBuilder: (context, index) {
            return createItem(context, index);
          })),
  );
 }
}

Widget createItem(BuildContext context, index) {
return Container(
height: MediaQuery.of(context).size.height * 0.3,
width: MediaQuery.of(context).size.width,
child: Text(modelProduct[index].title),
color: Color(modelProduct[index].color),
);
}

the issue is on color: Color(modelProduct[index].color) this line , and the error is

The argument type 'Color' can't be assigned to the parameter type 'int'.

but i know that if i turn the type of color into int in my model class, and provide the int type value of color like 0xFFFFFF then the error is resolved, but my question is if i want to use the material color like i used above, how to do with it. Thank you

3

3 Answers

2
votes

You can directly use color: modelProduct[index].color

code snippet

return Container(
  height: MediaQuery.of(context).size.height * 0.3,
  width: MediaQuery.of(context).size.width,
  child: Text(modelProduct[index].title),
  color: modelProduct[index].color,
);
0
votes

Try the below code :

Container( 
  height: MediaQuery.of(context).size.height* 0.3,
  width: MediaQuery.of(context).size.width,
   child: Text(modelProduct[index].title),
   color: modelProduct[index].color
   );
0
votes

below works for me

return Container(
  height: MediaQuery.of(context).size.height * 0.3,
  width: MediaQuery.of(context).size.width,
  child: Text(modelProduct[index].title),
  color: modelProduct[index].color.value,
);

It takes the int value of the color