错误:当试图将静态成员放入卡片标题行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'),
),
);
}

最新更新