Flutter : NoSuchMethodError: The getter 'nom' was called on null. Receiver; null Tried calling: nom

huangapple go评论84阅读模式
英文:

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&lt;TestSQFLITE&gt; {

  DatabaseHelper helper = DatabaseHelper();

  PanierModel _panier;

  @override
  Widget build(BuildContext context) {


    setState(() {
       _panier.nom  = _panier.nom  == null ? &quot;Arduino&quot; : &quot;&quot;;

    });

    void _ajouterPanier() async {
      int result;
      result = await helper.insertPanier(_panier);
      if(result != 0)
        print(&#39;STATUS Panier Save Successfully&#39;);
    }

return Scaffold(
  body: Container(
    child: Center(
      child: Column(
        children: &lt;Widget&gt;[
          Text(_panier.nom , style: TextStyle(color: Colors.red, fontWeight: FontWeight.bold),),
          Text(&quot;Abibou&quot;, 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

huangapple
  • 本文由 发表于 2020年4月6日 18:15:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/61057474.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定