Flutter:TextEditingController在初始化错误中只能访问静态成员

时间:2019-08-09 12:52:46

标签: flutter dart

我正在将数据从listmosque.dart传递到update_screen.dart以更新数据。 一切正常,但我想设置textedittingcontroller的默认值。

TextEditingController _txtnrp = TextEditingController(text: "${widget.nrpUpdate}"); <- this error

我在Only static members can be accessed in initializers.中收到消息${widget.nrpUpdate} 该如何解决?

这是我的update_screen.dart

import 'package:flutter/material.dart';

class UpdateScreen extends StatefulWidget {
  final String idUpdate;
  final String nrpUpdate;
  final String namaUpdate;
  final String emailUpdate;
  final String jurusanUpdate;
  UpdateScreen(
      {this.idUpdate,
      this.nrpUpdate,
      this.namaUpdate,
      this.emailUpdate,
      this.jurusanUpdate});
  @override
  _UpdateScreenState createState() => _UpdateScreenState();
}

class _UpdateScreenState extends State<UpdateScreen> {

 TextEditingController _txtnrp = TextEditingController(text: "${widget.nrpUpdate}"); <- In this line error

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: Icon(Icons.arrow_left),
          onPressed: () => Navigator.pop(context, false),
        ),
        centerTitle: true,
        title: Text('Update ${widget.namaUpdate}'),
      ),
      body: Container(
        child: Center(
          child: SingleChildScrollView(
            padding: EdgeInsets.all(20),
            scrollDirection: Axis.vertical,
            child: Column(
              children: <Widget>[
                TextField(

                  controller: _txtnrp,
                  keyboardType: TextInputType.number,
                  decoration: InputDecoration(hintText: "NRP"),
                  textAlign: TextAlign.center,
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

如何解决此问题? 谢谢

2 个答案:

答案 0 :(得分:1)

将其移动到function getConnection(){ mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); return (new mysqli('p:xxxx','yyy','zzz','aaaa')); }

initState

答案 1 :(得分:0)

安德烈(Andrey)的answer是正确的,但只想添加一些细节:

确实存在问题,您正在尝试初始化State.widget之前,建议通过State.widget访问这些参数(而不是通过State构造函数)。

build()方法总是在状态更改时被调用,因此,每次状态更改时,将控制器设置为${widget.nrpUpdate}实际上会使文本字段表现出奇怪的行为(将其重置)。因此,这就是为什么您应该在initState()中设置此值的另一个原因。这样,例如,即使您不希望每次单击按钮,也会清除文本字段:

class _UpdateScreenState extends State<UpdateScreen> {
  TextEditingController _txtnrp;
  int _count = 1;

  Widget GetColumn(someText) {
    print(someText);
    return 
  }

  @override
  void initState() {
    _txtnrp = TextEditingController(text: "${widget.nrpUpdate}");
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: Icon(Icons.arrow_left),
          onPressed: () => Navigator.pop(context, false),
        ),
        centerTitle: true,
        title: Text('Update ${widget.namaUpdate}'),
      ),
      body: Container(
        child: Center(
          child: SingleChildScrollView(
            padding: EdgeInsets.all(20),
            scrollDirection: Axis.vertical,
            child: Column(
              children: <Widget>[
                TextField(
                  controller: TextEditingController(text: someText),
                  keyboardType: TextInputType.number,
                  decoration: InputDecoration(hintText: "NRP"),
                  textAlign: TextAlign.center,
                ),
                FlatButton(
                  child: Text("Click"),
                  onPressed: () => setState(() {_count += 1;})
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

除此之外,您可能希望在代码变大时对其进行组织,将其分解为函数和变量。这样做时,您可能会遇到类似的“仅静态对象”错误。一种避免这种情况的方法是:

class _UpdateScreenState extends State<UpdateScreen> {

    TextEditingController _txtnrp;

    Widget getColumn(someText)
      return Column(
        children: <Widget>[
          TextField(           
            controller: _txtnrp,
            keyboardType: TextInputType.number,
            decoration: InputDecoration(hintText: "NRP"),
            textAlign: TextAlign.center,
          ),
        ],
      );
    }

    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: ...,
        body: Container(
          child: Center(
            child: SingleChildScrollView(
              padding: EdgeInsets.all(20),
              scrollDirection: Axis.vertical,
              child: GetColumn("${widget.nrpUpdate}").                 
              ),
            ),
          ),
        ),
      );

    @override
    void initState() {
        _txtnrp = TextEditingController(text: "${widget.nrpUpdate}");
        super.initState();
    }