【问题标题】:How to add data to ListView from another page/class in Flutter如何从 Flutter 中的另一个页面/类向 ListView 添加数据
【发布时间】:2020-07-21 18:32:51
【问题描述】:

好的,我在 dart/flutter 中只有几页,在第三页我想将数据添加到列表视图女巫在第一页? 我有这样的模型类:

class Doors_Model {
  final String image;
  final String name;
  final String desc;

  Doors_Model({this.image, this.name, this.desc});

  static List<Doors_Model> allDevices() {
    var listOfAllDoors = new List<Doors_Model>();

    listOfAllDoors.add(
        new Doors_Model(image: 'door.png', name: 'name', desc: 'description'));

    return listOfAllDoors;
  }
}

让我们在第三类/页面中说我有一些数据要通过按钮单击传递: 因此,当我单击按钮时,我需要根据模型填充列表视图...

class AddDevices extends StatefulWidget {
  @override
  _AddDevicesState createState() => _AddDevicesState();
}

class _AddDevicesState extends State<AddDevices> {
  @override
  Widget build(BuildContext context) {
      child: Material(   
         
          child: Scaffold(       
         

                        floatingActionButton : FloatingActionButton(
                onPressed: (){

            _addDataToListView();

                }, elevation: 3,child: Icon(Icons.add),
              )
          ),
        ),
      ),

在 listView 页面上我有:

...
class _HomeState extends State<Home> {
  final List<Doors_Model> _allDevices = Doors_Model.allDevices();
...
  Widget _getList(BuildContext context, int index) {
    return Card( 
      child: Column(
        children: <Widget>[
          ListTile(
            leading: Image.asset(
              "assets/" + _allDevices[index].image,            
            ),
            title: Text(_allDevices[index].name),
            subtitle: Text(_allDevices[index].desc),
            trailing: Text('')          
             
              ),
            ),
          
          )
        ],
      ),
    );
  }

【问题讨论】:

    标签: flutter listview navigation


    【解决方案1】:

    最简单的方法,只需使用sharedpreference保存数据,从第一页读取这些数据信息

    【讨论】:

    • 感谢您的建议,您还有其他方法吗..?
    猜你喜欢
    • 2019-07-19
    • 1970-01-01
    • 2020-05-07
    • 2020-04-02
    • 2022-01-15
    • 1970-01-01
    • 2019-08-01
    • 2021-03-26
    • 1970-01-01
    相关资源
    最近更新 更多