在小部件树中检测到重复的GlobalKey或多个小部件使用了相同的GlobalKey

时间:2019-12-18 08:55:00

标签: flutter dart

我尝试创建表单以使用rest服务从文本字段向数据库输入内容,从第一次尝试起就可以正常工作,但是下次尝试时出现错误,该错误表示GlobalkKey出了点问题,所以可能就在这里。

class _DeliveryRecieverState extends State<DeliveryReciever> {
  List datas;
  File imagePath;
  bool validasi = false;
  String namaPenerima;
  final _keyFormReceiver = GlobalKey<FormState>();


  @override
  Widget build(BuildContext context) {
    datas = this.datas;
    print(datas);

    return DefaultTabController(
        length: 1,
        child: Scaffold(
          backgroundColor: Colors.white,
          appBar: AppBar(
            bottom: TabBar(
              tabs: <Widget>[
                Tab(
                  icon: Icon(Icons.local_shipping),
                  text: "Bukti Delivery",
                ),
              ],
            ),
            elevation: 0.1,
            // backgroundColor: Colors.cyan[800],
            flexibleSpace: Container(
              decoration: BoxDecoration(
                gradient: LinearGradient(
                    colors: [Color(0xFF17ead9), Color(0xFF6078ea)]),
              ),
            ),
            title: Text(
              "Delivery",
              style: TextStyle(
                fontFamily: "Popins-Bold",
                fontSize: ScreenUtil.getInstance().setSp(46),
                letterSpacing: .6,
                fontWeight: FontWeight.bold,
              ),
            ),
            actions: <Widget>[
            ],
          ),
          body: TabBarView(
            children: <Widget>[
              SingleChildScrollView(
                child: Padding(
                  padding: EdgeInsets.only(
                      left: 15.0, right: 15.0, top: 15.0, bottom: 15.0),
                  child: Column(
                    children: <Widget>[
                      Container(
                        width: double.infinity,
                        height: ScreenUtil.getInstance().setHeight(470),
                        decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(8.0),
                            boxShadow: [
                              BoxShadow(
                                  color: Colors.black12,
                                  offset: Offset(0.0, 15.0),
                                  blurRadius: 15.0),
                              BoxShadow(
                                  color: Colors.black12,
                                  offset: Offset(0.0, -10.0),
                                  blurRadius: 10.0)
                            ]),
                        child: Padding(
                            padding: EdgeInsets.only(
                                left: 16.0, right: 16.0, top: 16.0),
                            child: Form(
                              key: _keyFormReceiver,
                              autovalidate: validasi,
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: <Widget>[
                                  Text(
                                    "Nama Penerima",
                                    style: TextStyle(
                                        fontSize:
                                            ScreenUtil.getInstance().setSp(30),
                                        fontFamily: "Poppins-Bold",
                                        letterSpacing: .6),
                                  ),
                                  SizedBox(
                                    height:
                                        ScreenUtil.getInstance().setHeight(30),
                                  ),                                  
                                  TextFormField(
                                    validator: validasiReceivername,
                                    onSaved: (String nama) {
                                      namaPenerima = nama;
                                    },
                                    decoration: InputDecoration(
                                        hintText: "Nama Penerima",
                                        hintStyle: TextStyle(
                                            color: Colors.grey,
                                            fontSize: 12.0)),
                                  ),
                                  SizedBox(
                                    height:
                                        ScreenUtil.getInstance().setHeight(50),
                                  ),
                                  Row(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: <Widget>[
                                      Padding(
                                        padding: const EdgeInsets.all(10.0),
                                        child: RaisedButton(
                                          elevation: 7.0,
                                          color: Colors.green,
                                          padding: EdgeInsets.all(20.0),
                                          child: Text("LANJUT"),
                                          textColor: Colors.white,
                                          shape: RoundedRectangleBorder(
                                              borderRadius:
                                                  BorderRadius.circular(10)),

                                          onPressed: () {
                                            parseData();
                                          },
                                        ),
                                      ),
                                    ],
                                  ),
                                ],
                              ),
                            )),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
          bottomNavigationBar: Container(
            height: 55.0,
            child: BottomAppBar(
              child: Container(
                decoration: BoxDecoration(
                  gradient: LinearGradient(
                      colors: [Color(0xFF17ead9), Color(0xFF6078ea)]),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    IconButton(
                      icon: Icon(Icons.home, color: Colors.white),
                      onPressed: () {
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                              builder: (context) => halamanUtama()),
                        );
                      },
                    ),
                    IconButton(
                      icon: Icon(Icons.nature_people, color: Colors.white),
                      onPressed: () {
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                              builder: (context) => UpcomingDelivery()),
                        );
                      },
                    ),
                    IconButton(
                      icon: Icon(Icons.local_shipping, color: Colors.white),
                      onPressed: () {
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                              builder: (context) => ListDelivery()),
                        );
                      },
                    ),
                  ],
                ),
              ),
            ),
          ),
        ));
  }

  String validasiReceivername(String value) {
    if (value.length == 0) {
      //Toast.show("Password more than 4 ", context,duration: Toast.LENGTH_SHORT, gravity: Toast.CENTER);
      return "Nama penerima tidak boleh kosong.";
    } else {
      return null;
    }
  }

  void parseData() async {
    if (_keyFormReceiver.currentState.validate()) {
      _keyFormReceiver.currentState.save();
      _keyFormReceiver.currentState.reset();

      Navigator.push(
          context,
          MaterialPageRoute(
              builder: (context) => CustomerSignatureProof(
                  created_name: widget.created_name,
                  wmsorders_id: widget.wmsorders_id,
                  imagePath: widget.imagePath,
                  imageName: widget.imageName,
                  receiverName: namaPenerima,
                  upcoming_id: widget.upcoming_id,
                  upcoming_sku: widget.upcoming_sku,
                  upcoming_sak: widget.upcoming_sak,
                  upcoming_qty: widget.upcoming_qty,
                  upcoming_shipstatid: widget.upcoming_shipstatid,
                  upcoming_picname: widget.upcoming_picname,
                  upcoming_pictelp: widget.upcoming_pictelp,
                  upcoming_ordermultipleid: widget.upcoming_ordermultipleid,
                  upcoming_orderdetid: widget.upcoming_orderdetid,
                  upcoming_coordinatorid: widget.upcoming_coordinatorid,
                  upcoming_shipmentid: widget.upcoming_shipmentid)));
    }
  }
}

这是终端机中的错误

enter image description here

如果有人遇到与我相同的问题,请给予指导。

0 个答案:

没有答案