简体   繁体   English

如何加载提供者列表 flutter

[英]how to load a list with providers flutter

I want to how to load saved list data on new dart file with provider.我想了解如何使用提供程序将保存的列表数据加载到新的 dart 文件中。

The below code is my list with provider.下面的代码是我的提供者列表。

'items' list will be added with integer items. “项目”列表将添加 integer 项目。

class ListProvider is in provider.dart file. class ListProvider 在 provider.dart 文件中。

and

Swapping function is in different dart file.交换 function 在不同的 dart 文件中。

How can Swapping use added data in 'items' list??交换如何使用“项目”列表中添加的数据?

class ListProvider with ChangeNotifier {
  List<int> items = [
    -1,
  ];

  void addItem(int item) {
    items.add(item);
    notifyListeners();
  }

  bool contains(int item) {
    bool answer = items.contains(item);
    return answer;
  }
}

IconButton(
                  onPressed: Swapping,
                  icon: Image.asset('assets/refresh.png'),
                  iconSize: 20,
                ),


Swapping() {
    Random random = new Random();
    var numberHats = random.nextInt(items.length);
    setState(() {
      hat = hat_items[items[numberHats]];
    });
  }

if you use provider you not should use setState you need to ChangeNotifierProvider Widget如果你使用提供者你不应该使用 setState 你需要 ChangeNotifierProvider Widget

     ChangeNotifierProvider(
          create: (context) => ListProvider(),
          child: Scaffold(
child:....
child:IconButton(
                  onPressed: Swapping,
                  icon: Image.asset('assets/refresh.png'),
                  iconSize: 20,
                ),
),
        ),

and edit your model change with keyword to extends in Model并使用关键字编辑您的 model 更改以在 Model 中扩展

class ListProvider extends ChangeNotifier 

more details check enter link description here更多细节检查在这里输入链接描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM