android - 错误:尝试将静态成员放入卡片标题第 18 行时,只能在初始化程序中访问它
问题描述
所以它不允许我在卡的标题中使用 name 并且我已经尝试将 name 设置为静态变量但是我在构建时仍然会出错。我已经列出了所有相关的代码行 // 非常感谢您的帮助
当我尝试将 name 设置为 static var name = "" 然后运行应用程序时,我应该添加卡名仍然显示为空白,因此它不会更新静态变量。为什么这样?有没有办法解决它?我该怎么做?
import 'package:flutter/material.dart';
import 'package:rflutter_alert/rflutter_alert.dart';
class CardApp extends StatefulWidget {
@override
_CardAppState createState() => _CardAppState();
}
class _CardAppState extends State<CardApp> with AutomaticKeepAliveClientMixin<CardApp> {
var _widgetsList = [];
final myController = new TextEditingController();
var name ;
var test =
Card(
child: new ListTile(
leading: Icon(Icons.album),
title: Text(name), //this line
subtitle: Text('this is the subtitle'),
),
);
@override
void dispose() {
// Clean up the controller when the widget is disposed.
myController.dispose();
super.dispose();
}
@override
// TODO: implement wantKeepAlive
bool get wantKeepAlive => true;
@override
Widget build(BuildContext context) {
super.build(context);
return MaterialApp(
home: Scaffold(
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
return Alert(
context: context,
title: 'Please Input your note',
desc: 'this is where you will input your note',
buttons: [
DialogButton(child: Text('Cancel'), onPressed:() {Navigator.pop(context);}),
DialogButton(child: Text('Confirm'), onPressed:() => setState(() //this line
{name = myController.text; // all of this
print(name);
_widgetsList.add(test);
Navigator.pop(context);}
)
//end
)
],
content: Form(child: Column(
children: <Widget>[
TextFormField(
controller: myController,
decoration: InputDecoration(labelText: 'Input what you want to say'),
)
],
))
).show();
}
),
appBar: AppBar(
backgroundColor: Colors.purple,
title: Text('The Note App', style: TextStyle(
color: Colors.white
),),
),
body: Container(
child: ListView(
children: <Widget>[
Card(
child: new ListTile(
leading: Icon(Icons.album),
title: Text('hello'),
subtitle: Text('hi'),
),
),
..._widgetsList, // cards get appended to _widgetlist so it appears up on the screen
],
),
),
),
);
}
}
解决方案
我想出了一个解决方案,而不是使用变量作为卡片我制作了一个名为 addCard 的函数来附加到我的小部件列表
addCard(){
return Card(
child: ListTile(
leading: Icon(Icons.album),
title: Text(name),
subtitle: Text('hi'),
),
);
}
推荐阅读
- javascript - addEventListener() 中 console.log() 的异常行为 - JAVASCRIPT
- typescript - 扩展一个`记录
` 具有不同类型的属性 - android - 在哪里可以找到已安装的 Android SDK 源代码的所有类?
- sql - 如何将 Oracle Sql 转换为 Postgresql
- c - 比较已定义和未定义的变量
- python - 如果线程遇到异常如何杀死进程?
- iis - iis - 通过多个域进行 Windows 身份验证
- asp.net-core-2.1 - 当我在单个 asp core 2.1 razor 页面上有编辑和新建表单时,如何规避服务器端模型验证错误?
- azure-devops - 如果 Azure DevOps 未将其检测为 TG 依赖项,如何公开任务组参数?
- angular - 检查数组中的重复对象