Flutter:在提供商槽功能中更新列表时,我的屏幕没有更新 [英] Flutter: My screen doesn't update when I update my List in the provider trough functions

查看:106
本文介绍了Flutter:在提供商槽功能中更新列表时,我的屏幕没有更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主文件中有一个提供者具有功能的列表。如果我通过小部件中的功能(通过按按钮)更新此列表,则它似乎可以工作,但在屏幕上没有任何更改...

I have a List with functions in a Provider in my Main File. If I update this List trough the functions in my widget (by pressing the buttons), it seems to work but doesn´t change anything on the screen...

我有两个屏幕,应该与同一列表一起提交,因此我需要提供程序功能。
在我看来(当然不一定正确),我对功能进行了编码,因此问题可能出在屏幕未更新,我用的是Consumer。

I have two Screens, which should be filed with the same List, so I need the Provider Functions. In my opinion (which of course don´t have to be right) I code the functions right, so the problem may be that the screens didn´t update, altaugh I used the Consumer. It would be great if you can show me the mistake in my Code.

如果您能看看我的问题,我将非常高兴-谢谢!

I would be very happy if you could look at my problem - Thank you!

MainFile:

void main() => runApp(
  MultiProvider(
  providers: [

    ChangeNotifierProvider(create: (_) => MyList()),

  ],

  child: MyApp(),
),);


class MyList extends ChangeNotifier {

 final List<String> heute = [
    'Ereignis 1',
    'Ereignis 2',
    'Ereignis 3',
    'Ereignis 4',
    'Ereignis 5'
  ];

  UnmodifiableListView<String> get items => UnmodifiableListView(heute);

  int get totallength => heute.length;

  void addItem(String item) {
      heute.add(item);
      notifyListeners();
  }

  void deleteItem(int index) {
    heute.removeAt(index);
    notifyListeners();
  }

  void removeAll() {
    heute.clear();
    notifyListeners();
  }

}

小部件:

class ContainerListe extends StatefulWidget {
  @override
  _ContainerListeState createState() => _ContainerListeState();

}
class _ContainerListeState extends State<ContainerListe> {

final heute = MyList();

  void addItem(String item) {
    setState(() {
    heute.addItem(item);});
    Navigator.of(context).pop();
  }

  void removeAll(){setState(() {
    setState(() {
    heute.removeAll();});
  });}

  void deleteItem(int index) {
  setState(() {
  heute.deleteItem(index);
  });}


  void newEntry() {
  showDialog<AlertDialog>(
  context: context, 
  builder: (BuildContext context) 
  {return AddItemDialogWHW(addItem);});}


  @override
  Widget build(BuildContext context) {
    final heute = MyList();
    final elemente = heute.items;


      return Consumer<MyList>(
          builder: (context, mylist, child)
    {
      return
        Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Container(
                  width: 400.0,
                  height: 550.0,
                  child: Align(
                      alignment: Alignment.bottomCenter,
                      child:

                      ListView.builder(
                        itemCount: Provider
                            .of<MyList>(context, listen: true)
                            .totallength,
                        itemBuilder: (context, i) {
                          return
                            Column(
                              children: [

                                ContainerVorlage(
                                  elemente[i],
                                      () => deleteItem(i),
                                ),

                                SizedBox(
                                  height: 15,
                                ),


                              ],
                            )

                          ;
                        },
                      )

                  )),


              SizedBox(
                height: 40,
              ),


              AddButton(
                    () => newEntry(),
              ),


            ]);
    });

  }
}


//More Code

推荐答案

首先,从 _ContainerListeState

First, remove this line from _ContainerListeState

final heute = MyList();

然后使用 MyList 对象中的方法使用

Then use the methods from the MyList object, as you can use

 return Consumer<MyList>(builder: (context, mylist, child)
 ... // rest  code
   ContainerVorlage(
     elemente[i],
     () => mylist.deleteItem(i),
  ),

这篇关于Flutter:在提供商槽功能中更新列表时,我的屏幕没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆