我為 sqlite 創建了一個演示,其中我創建了一個模型類類別模型,并且在 sqlite 中我在其 on create 方法中創建了一些默認類別,現在想使用未來的構建器在主螢屏上顯示所有類別,但它的執行錯誤部分是功能構建器..和應用程式以中心文本開頭
type int is not a subtype of string
類別模型
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'])
);
}
}
這是我使用 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');
}
},
);
}
這是我的資料庫助手類
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;
}
}
uj5u.com熱心網友回復:
嘗試轉換為字串。
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,
);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/518260.html
標籤:扑镖
