Flutter:NoSuchMethodError:在null上调用了getter'nom'。接收器;尝试调用:nom

时间:2020-04-06 10:15:44

标签: java android ios flutter dart

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

NoSuchMethodError:在null上调用了吸气剂“ nom”。接收器; null尝试调用: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 :(得分:0)

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

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