8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

更新状态时如何避免重复查找状态?

Nadir Esmail 1月前

53 0

这里我有 Flutter 提供程序类 Page 的代码,扩展了 StatelessWidget { GlobalState? _state; GlobalState get state => _state!; set state(GlobalState value) => _state ??= value; Wi...

这里我有 Flutter 提供程序的代码

class Page extends StatelessWidget {
  GlobalState? _state;
  GlobalState get state => _state!;
  set state(GlobalState value) => _state ??= value;

  Widget build(BuildContext context) {
      state = context.read<GlobalState>();
      return Selector<GlobalState, int> (
         selector: (_, innerState) => innerState.intValue,
         builder: (_, value, child) => ...
      );
   }
}

我确信状态将在首次构建时初始化,下次程序将发现状态不为空并且不会匹配 context.read<GlobalState> ,但是在 Selector ,每次当状态时 notifyListener Selector 总会再次寻找相同的 GlobalState ,我该如何改进我的代码?

我认为我已经找到了 的位置 GlobalState ,因此无需 GlobalState 再次寻找 ,我不知道如何在 中修复它 Provider ,或者我应该转向 getx

帖子版权声明 1、本帖标题:更新状态时如何避免重复查找状态?
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由Nadir Esmail在本站《flutter》版块原创发布, 转载请注明出处!
最新回复 (0)
  • 这是 ChangeNotifier 您的案例的示例。您可以划分小的Widget并直接返回 Selector 如果您不想在状态更改时更新小部件,则 shouldRebuild 可以使用 Selector

    import 'package:flutter/foundation.dart';
    import 'package:flutter/material.dart';
    import 'package:provider/provider.dart';
    
    void main() {
      runApp(
        MultiProvider(
          providers: [
            ChangeNotifierProvider(create: (_) => Counter()),
          ],
          child: const MyApp(),
        ),
      );
    }
    
    class Counter with ChangeNotifier {
      int _count = 0;
    
      int get count => _count;
    
    
      /// This function only use when you need change state, 
      /// but doesn't want to re-render all widget that get this state
      // void increment(bool isNotify) {
      //   _count++;
      //   if (isNotify) {
      //     notifyListeners();
      //   }
      // }
    
      void increment() {
        _count++;
        notifyListeners();
      }
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return const MaterialApp(
          home: MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatelessWidget {
      const MyHomePage({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('Example'),
          ),
          body: Center(
            child: Selector<Counter, int>(
              builder: (context, data, child) {
                return Container(
                  height: 40,
                  color: Colors.red,
                  child: Center(child: Text('Consumer1: $data')),
                );
              },
              selector: (buildContext, state) {
                return state.count;
              },
              shouldRebuild: (prevState, nextState) {
                //Change logic update widget when state change at here.
                // Return false when widget only get state once time.
                return false;
              }
            ),
          ),
          floatingActionButton: FloatingActionButton(
            key: const Key('increment_floatingActionButton'),
    
            /// Calls `context.read` instead of `context.watch` so that it does not rebuild
            /// when [Counter] changes.
            onPressed: () => context.read<Counter>().increment(),
            tooltip: 'Increment',
            child: const Icon(Icons.add),
          ),
        );
      }
    }
    
返回
作者最近主题: