I creating a demo for sqlite where I have created a model class category model and in sqlite I have created some default categories in its on create method and now want to show all categories on home screen using future builder but its execute error part of feature builder..and app starts with center text
type int is not a subtype of string
category model
class CategoryModel{
String title;
IconData icon;
int entries;
double totalamount;
CategoryModel({required this.title,this.entries=0,required this.icon,this.totalamount=0.0});
Map<String,dynamic> tomap()
{
return {
'title':title,
'entries':entries,
'totalamount':totalamount.toString()
};
}
factory CategoryModel.frommap(Map<String,dynamic> map)
{
return CategoryModel(
title: map['title'],
icon: icons[map['title']]!,
entries: map['entries'],
totalamount: double.parse(map['totalamount'])
);
}
}
this is the widget where I am using futurebuilder
Widget getbody()
{
return FutureBuilder(
future: SqfliteProvider.fetchcategory(),
builder: (context,AsyncSnapshot<List<CategoryModel>?>snapshot){
if (snapshot.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator();
} else if (snapshot.hasError) {
return Center(
child: Text(snapshot.error.toString() ' Founded'),
);
} else if (snapshot.hasData) {
if (snapshot.data != null) {
return ListView.builder(
itemCount: snapshot.data!.length,
itemBuilder: (context, index) {
CategoryModel t = snapshot.data![index];
print(t.title.toString());
print(' ');
print(t.entries.toString());
print(' ');
print(t.totalamount.toString());
return Card(
child: ListTile(
leading: CircleAvatar(
child: Icon(icons[t.title]),
),
title: Text(t.title.toString(),style: TextStyle(fontSize: 18,fontWeight: FontWeight.bold),),
subtitle:Text(
"Entries :" t.entries.toString(),
style: TextStyle(fontSize: 14),
) ,
),
);
});
} else {
return Text('Something wrong');
}
} else {
return Text('Last if');
}
},
);
}
this is my database helper class
class SqfliteProvider {
static Future<Database> _getdb() async {
return openDatabase(join(await getDatabasesPath(), 'expensedb1.db'),
version: 1,
onCreate: (db,version) {
db.execute("CREATE TABLE categorytb(title TEXT NOT NULL,entries INTEGER NOT NULL,totalamount STRING NOT NULL)");
//add some default categories
print('I am ready to call');
for(int x=0;x<icons.length;x )
{
db.insert('categorytb', {
'title':icons.keys.toList()[x],
'entries':0,
'totalamount':(0.0).toString()
});
}
});
}
static Future<List<CategoryModel>?> fetchcategory() async{
final db=await _getdb();
List<CategoryModel> templist=[];
List<Map<String,dynamic>> maplist=await db.query('categorytb');
print(maplist.toString());
if(maplist.isEmpty)
return null;
templist=maplist.map((e) => CategoryModel.frommap(e)).toList();
return templist;
}
}
CodePudding user response:
Try to convert toString.
factory CategoryModel.frommap(Map<String, dynamic> map) {
IconData getIconData(data) {
try {
return IconData(data);
} catch (e) {
return IconData(Icons.abc.codePoint);
}
}
return CategoryModel(
title: map['title'].toString(),
icon: getIconData(map['title']), // pass different data if needed
entries: int.tryParse(map['entries'].toString()) ?? 0,
totalamount: double.tryParse(map['totalamount']) ?? 0,
);
}