英文:
Flutter : NoSuchMethodError: The getter 'nom' was called on null. Receiver; null Tried calling: nom
问题
class _TestSQFLITEState extends State<TestSQFLITE> {
DatabaseHelper helper = DatabaseHelper();
PanierModel _panier = PanierModel(); // Initialize _panier with an instance of PanierModel
@override
Widget build(BuildContext context) {
setState(() {
_panier.nom = _panier.nom == null ? "Arduino" : "";
});
void _ajouterPanier() async {
int result;
result = await helper.insertPanier(_panier);
if(result != 0)
print('STATUS Panier Save Successfully');
}
return Scaffold(
body: Container(
child: Center(
child: Column(
children: <Widget>[
Text(_panier.nom , style: TextStyle(color: Colors.red, fontWeight: FontWeight.bold),),
Text("Abibou", style: TextStyle(color: Colors.red, fontWeight: FontWeight.bold),),
],
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: _ajouterPanier,
child: Icon(Icons.add, color: Colors.white,),
),
);
}
}
英文:
I try to assign value to the attributes of the PanierModel but I receive this error on picture
NoSuchMethodError: The getter 'nom' was called on null. Receiver; null Tried calling: nom
class _TestSQFLITEState extends State<TestSQFLITE> {
DatabaseHelper helper = DatabaseHelper();
PanierModel _panier;
@override
Widget build(BuildContext context) {
setState(() {
_panier.nom = _panier.nom == null ? "Arduino" : "";
});
void _ajouterPanier() async {
int result;
result = await helper.insertPanier(_panier);
if(result != 0)
print('STATUS Panier Save Successfully');
}
return Scaffold(
body: Container(
child: Center(
child: Column(
children: <Widget>[
Text(_panier.nom , style: TextStyle(color: Colors.red, fontWeight: FontWeight.bold),),
Text("Abibou", style: TextStyle(color: Colors.red, fontWeight: FontWeight.bold),),
],
),
),
),
floatingActionButton: FloatingActionButton(onPressed: _ajouterPanier,
child: Icon(Icons.add, color: Colors.white,),),
);
}
}
答案1
得分: 0
你的问题是你没有初始化你的 _panier,这就是为什么你会得到错误,因为在空的 _panier 上调用了 _panier.nom,由于 _panier 为空,空对象没有任何属性。你需要执行以下步骤:
PanierModel _panier = PanierModel(); //现在 _panier 不再为空
英文:
Your problem is that you are not initialising your _panier and that's why you get the error because _panier.nom is called on null as _panier is null and null object doesn't have any attributes. You have to do the following:
PanierModel _panier = PanierModel(); //now _panier is not null anymore
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论