【问题标题】:Provider trying to update my count when ever will I receive Notification?提供商试图更新我的计数我何时会收到通知?
【发布时间】:2021-08-15 11:27:20
【问题描述】:

我无法更新我的用户界面

//// 我正在控制台中获取更新数据,但无法在我的 UI 中进行更新

我的 NotificationCountClass

class NotificationCount extends ChangeNotifier{
   var count;

   NotificationCount({
     this.count =0,
   });

   addNotificationCount(){
    count++;
    notifyListeners();
    print("Notification Count $count"); 
  }

}

main :这里我将小部件包装在 multiprovider 中,以便我可以在我的应用程序中的任何位置使用它

return MultiProvider(
          providers: [
            ChangeNotifierProvider(create: (context) => AppService()),
            ChangeNotifierProvider(create: (context) => NotificationCount()),
          ],
          child: Consumer<AppService>(
            builder: (context, appService, child) {
              return GetMaterialApp(
                title: AppStrings.APP_TITLE,
                theme: AppTheme.lightTheme,
                darkTheme: AppTheme.dartTheme,
                navigatorKey: GlobalVariable.navigatorKey,
                supportedLocales: [
                  Locale('en'),
                ],
                localizationsDelegates: [
                  CountryLocalizations.delegate,
                ],
                themeMode: appService.isDarkMode ? ThemeMode.dark : ThemeMode.light,
                initialRoute: AppRouter.SPLASH_SCREEN,
                onGenerateRoute: AppRouter.router.generator,
                // routes: {
                //   "/":(context) =>Home(),
                //   "/AppChat" : (context) => AppChat(),
                // },
                debugShowCheckedModeBanner: false,
                // home: AppChat(),
              );
            },
          ),
        );

// 将 State 与 Consumer 小部件一起使用,因此只需要重建 wiget

Consumer<NotificationCount>(
                                              builder: (context, value, child) {
                                                var count = value.count;
                                                print("Count of Not : $count");
                                                return Text(
                                                  "$count",
                                                  style: TextStyle(
                                                    color: Colors.white,
                                                  ),
                                                );
                                              },
                                            ),

通过提供者获取 NotificationCount 类但仍无法更新 UI

 final notificationCount = Provider.of<NotificationCount>(context , listen: false);

【问题讨论】:

    标签: flutter provider state-management


    【解决方案1】:

    我可以在我这边重现您的问题,并且可以通过使用 MultiProvider 的构建器而不是 child 来解决它。

    而不是

    return MultiProvider(
              providers: [
                ChangeNotifierProvider(create: (context) => AppService()),
                ChangeNotifierProvider(create: (context) => NotificationCount()),
              ],
              child: Consumer<AppService>(
    

    这样写:

    return MultiProvider(
            providers: [
              ChangeNotifierProvider(create: (context) => AppService()),
              ChangeNotifierProvider(create: (context) => NotificationCount()),
            ],
            builder: (context, _) => Consumer<AppService>(
    ...
    

    【讨论】:

    • @fakhar-ilyas,​​这个答案解决了你的问题吗?
    猜你喜欢
    • 1970-01-01
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多