gpt4 book ai didi

flutter : TextEditingController Only Static member can be accessed in initializer error

转载 作者:IT王子 更新时间:2023-10-29 06:55:54 26 4
gpt4 key购买 nike

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

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

我收到消息 Only static members can be accessed in initializers. in ${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,
),
],
),
),
),
),
);
}
}

如何解决这个问题?谢谢

最佳答案

将其移入initState:

class _UpdateScreenState extends State<UpdateScreen> {

TextEditingController _txtnrp;

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

关于 flutter : TextEditingController Only Static member can be accessed in initializer error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57430474/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com