我尝试将值分配给PanierModel的属性,但在图片上收到此错误

NoSuchMethodError:在空调用getter'nom'。接收器;尝试调用: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,),),
);
  }
 }

最佳答案

您的问题是您没有初始化_panier,这就是为什么会出现错误,因为_panier.nom在_panier为null且null对象没有任何属性的情况下在null上被调用。您必须执行以下操作:

PanierModel _panier = PanierModel(); //now _panier is not null anymore

10-08 18:26