首页 > 解决方案 > 错误:尝试将静态成员放入卡片标题第 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
              ],

            ),





          ),

        ),

    );
  }

}

标签: androidflutter

解决方案


我想出了一个解决方案,而不是使用变量作为卡片我制作了一个名为 addCard 的函数来附加到我的小部件列表

addCard(){
    return Card(
      child: ListTile(
        leading: Icon(Icons.album),
        title: Text(name),
        subtitle: Text('hi'),
      ),
    );
  }

推荐阅读