Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
颤振:`notifyListeners`不更新列表_List_Flutter_Provider - Fatal编程技术网

颤振:`notifyListeners`不更新列表

颤振:`notifyListeners`不更新列表,list,flutter,provider,List,Flutter,Provider,我正在尝试列出我的应用程序中的产品类别,并使用提供商软件包进行状态管理。在构建期间,它显示列表是空的,即使它不是空的,然后我添加了一个click事件来更新它工作时的列表。 我在启动屏幕期间调用getAllCategories()函数,vallCategoryList具有值 这是我的分类课 class Category with ChangeNotifier{ int catId; String catName; String catThumbnail; List<Set

我正在尝试列出我的应用程序中的产品类别,并使用提供商软件包进行状态管理。在构建期间,它显示列表是空的,即使它不是空的,然后我添加了一个click事件来更新它工作时的列表。 我在启动屏幕期间调用
getAllCategories()
函数,
vallCategoryList
具有值

这是我的分类课

class Category with ChangeNotifier{

  int catId;
  String catName;
  String catThumbnail;

  List<SetCategory> allCategoryList = [];


void  getAllCategories() async {
    String categoryUrl = 'https://app.ecwid.com/api/ccccccc';

    Response allCategory = await get(categoryUrl);

    print('getAllCategories');


    if (allCategory.statusCode == 200) {
      var categoryData = allCategory.body;

      int totalcount = jsonDecode(categoryData)['count'];

      if (allCategoryList.length != totalcount) {
        allCategoryList.clear();

        for (int i = 0; i < totalcount; i++) {
          allCategoryList.add(SetCategory(
            catId: jsonDecode(categoryData)['items'][i]['id'],
            catName: jsonDecode(categoryData)['items'][i]['name'],
            catThumbnail: jsonDecode(categoryData)['items'][i]['thumbnailUrl'],
          ));

        }

      }
    }
    print('allcategorylist length ${allCategoryList.length}');

    notifyListeners();
  }



}

class SetCategory {

  int catId;
  String catName;
  String catThumbnail;

  SetCategory(
      { this.catId, this.catName, this.catThumbnail});

}
带有ChangeNotifier的类类别{
int catId;
字符串catName;
弦指甲;
列出allCategoryList=[];
void getAllCategories()异步{
字符串类别https://app.ecwid.com/api/ccccccc';
响应allCategory=等待获取(categoryUrl);
打印('getAllCategories');
如果(allCategory.statusCode==200){
var categoryData=allCategory.body;
int totalcount=jsonDecode(categoryData)['count'];
if(allCategoryList.length!=totalcount){
allCategoryList.clear();
对于(int i=0;i
我的屏幕代码

class HomeScreen extends StatefulWidget {
  static const String id = 'homeScreen';
//  static int reload = 0;



  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
 @override
  Widget build(BuildContext context) {

      final category = Provider.of<Category>(context);

    print('category length ${category.allCategoryList.length}'); // here it shows length as 0 even though it has a value of 16.

    return Row(
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Text(
                      'Category ${category.allCategoryList.length}',
                      style: TextStyle(
                        color: Colors.black,
                        fontWeight: FontWeight.bold,
                        fontSize: 15.0,
                      ),
                      textAlign: TextAlign.start,
                    ),
                  ),

                  InkWell(
                                      onTap: () {

                                        category.getAllCategories();  // when i tap here this updates the list
                                        }),
                ],
              ),

类主屏幕扩展StatefulWidget{
静态常量字符串id='主屏幕';
//静态int重载=0;
@凌驾
_HomeScreenState createState()=>\u HomeScreenState();
}
类_homescrenstate扩展状态{
@凌驾
小部件构建(构建上下文){
最终类别=提供者(上下文);
print('category length${category.allCategoryList.length}');//在这里,它将长度显示为0,即使它的值为16。
返回行(
儿童:[
填充物(
填充:常数边集全部(8.0),
子:文本(
'Category${Category.allCategoryList.length}',
样式:TextStyle(
颜色:颜色,黑色,
fontWeight:fontWeight.bold,
字体大小:15.0,
),
textAlign:textAlign.start,
),
),
墨水池(
onTap:(){
category.getAllCategories();//当我点击此处时,会更新列表
}),
],
),

您是否在小部件中使用了
ChangeNotifierProvider
,如图所示


如果您刚刚使用了
提供程序
,则它不会更新,而只是使对象可以从子体访问

您是否在小部件中使用了
更改通知提供程序


如果您刚刚使用了
提供程序
,则它不会进行更新,而只是使对象可以从子体访问

通过添加
消费者
解决,更改代码如下

child: Consumer<Category>(
                        builder: (_,category,__){
                          return ListView.builder();
}
子项:消费者(
建筑商:(uu,类别,uuu){
返回ListView.builder();
}

通过添加
消费者
解决,更改代码如下

child: Consumer<Category>(
                        builder: (_,category,__){
                          return ListView.builder();
}
子项:消费者(
建筑商:(uu,类别,uuu){
返回ListView.builder();
}

Yes我在main.dart文件中添加了它,如下所示
Widget build(BuildContext context){return ChangeNotifierProvider(builder:(context)=>Category(),child:MaterialApp(initialRoute:customsplash.SplashScreen.id,routes:{customsplash.SplashScreen.id:(context)=>customsplash.SplashScreen(),HomeScreen.id:(context)=>HomeScreen(),},,);
Yes我在main.dart文件中添加了它,如下所示
Widget build(BuildContext context){return ChangeNotifierProvider(builder:(context)=>Category(),child:MaterialApp(initialRoute:customsplash.SplashScreen.id,routes:{customsplash.SplashScreen.id:(上下文)=>customsplash.SplashScreen(),HomeScreen.id:(上下文)=>HomeScreen(),},),);